2013-10-20 69 views
2

我有一个包含一个listview的相对视图。我在运行时创建这个相对视图并将内容(基本上是一个字符串数组)添加到我的列表视图中。我已经将listview的宽度和高度设置为“wrap_content”。 问题是我的列表父级相对视图不调整大小以显示我的列表视图。它只是显示列表的第一个值。 我明白我必须重写onmeasure方法。那么,我怎样才能在onmeasure方法中获得我的listview的高度,所以我可以将它传递给我的父视图 我已经阅读了许多像这样的问题,但无法使其工作。如何更改父视图的高度以适合子视图

还有一件事getMeasuredHeight()在我的onmeasure()方法中返回0。

更新

我主要布局activity_main

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/layout_route_search" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<RelativeLayout 
    android:id="@+id/layout_main" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</RelativeLayout> 

</ScrollView> 

lo_block.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Text_view" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

这里是我的代码
Frag.java

public class Frag extends Fragment { 

private View mView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    mView = inflater.inflate(R.layout.activity_main, null); 

    showView(); 
    return mView; 
} 

public void showView() { 

    block card = new block(getActivity()); 

    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT); 

    card.setLayoutParams(params); 
    ViewGroup vg = (ViewGroup) mView.findViewById(R.id.layout_main); 
    vg.addView(card); 
} 

private class block extends RelativeLayout { 

// int listViewh = 0; // private float listViewHeight;

public block(Context context) { 
     super(context); 

     View mView = View.inflate(getContext(), R.layout.lo_block, this); 

     ListView lst = (ListView) mView.findViewById(R.id.listView1); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       getActivity(), android.R.layout.simple_list_item_1, 
       new String[] { "ad", "sfsd", "sfsf", "asfsfs" }); 

     lst.setAdapter(adapter); 
    } 

    public block(Context context, AttributeSet attrs, int defStyle) { 

     super(context, attrs, defStyle); 

    } 

    @Override 
    public int getMinimumHeight() { 

     return super.getMinimumHeight(); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 

    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int w = MeasureSpec.getSize(widthMeasureSpec); 
     int h = MeasureSpec.getSize(heightMeasureSpec); 

// setMeasuredDimension(w,h); }

} 

}

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/fragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

</LinearLayout> 

我的主要活动 公共类MainActivity在我的情况下延伸活动{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    getFragmentManager().beginTransaction().replace(R.id.fragment, 
      new Frag()).commit(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


} 
+0

忘记提及了,Relativelayout的宽度和高度也是wrap_content。 –

+0

结果也是如此。只是为了说明,如果将我的列表视图的高度设置为某些像素表示“250dp”,那么我的相对布局显示,所以唯一的问题是在它呈现自身之前将相对视图的计算高度给予相对视图。 –

+0

不完全是这样,它在正常的活动下工作正常,但如果我把这段代码放在一个片段中,那么它就不会。也尝试这一次与片段。主要活动xml包含一个带有线性布局的框架布局。 –

回答

1

确切的问题是我想将listview放入scro中llview。和那个一般不推荐。但是我设法扩展了我的列表视图。尽管之前它不能更好地滚动。下面是我在我的构造函数中添加的代码

ListView lst = (ListView) mView.findViewById(R.id.listView1); 
     String[] arrayList = new String[] { "ad", "sfsd", "sfsf", "asfsfs", 
       "ssfsff", "llkllk", "4323" }; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       getActivity(), android.R.layout.simple_list_item_1, 
       arrayList); 
     int totalHeight = 0; 
     for (int i = 0; i < adapter.getCount(); i++) { 
      View listItem = adapter.getView(i, null, lst); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 
     LayoutParams lp = (LayoutParams) lst.getLayoutParams(); 
     int height = totalHeight; 

     // arraylist list is in which all data is kept 

     lp.height = height; 
     lst.setLayoutParams(lp); 

     lst.setAdapter(adapter); 
相关问题