如何设置ListView的正确尺寸?

时间:2012-11-12 14:26:52

标签: android android-layout android-listview android-linearlayout android-layout-weight

我需要在简单的线性布局中设置listView的尺寸。我在示例belove中设置了权重,但是出了点问题:textView似乎没有设置,列表占用了更大的维度。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:gravity="center"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="0.7">
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="0.1"
        android:text="Avanti" />

</LinearLayout>

如何纠正最终错误或做得更好?

这就是我所拥有的:

2 个答案:

答案 0 :(得分:0)

我不认为在Button上或TextView上使用layout_weight是个好主意。现在,我不太确定你想要它看起来如何,但这就是我将如何做到这一点:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Avanti" />
</LinearLayout>

这里要更改的主要值是ListView的layout_weight和LinearLayout的weightSum,但在我看来好像你希望它填充视图的其余部分,按钮和TextView就位。如果您对上述结果不满意,请使用layout_weight和weightSum。

答案 1 :(得分:0)

一种可能的方法是设置textview的高度(例如,我通常设置textSize,然后将height设置为textsize * 3.7)

例如:

   <TextView
            android:id="@+id/lexplicacionsNumPagText"
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="right"
            android:gravity="right"
            android:layout_weight="2.10"
            android:scrollHorizontally="false"
            android:singleLine="true"
            android:text="01/05"
            android:textColor="#5d8a3b"
            android:textSize="10.5dip"
            android:textStyle="bold" />

如果这对你没有帮助......解决方案可能是以编程方式设置高度..也许这不是最好的方式,但在maaany问题之后,它多次保存了我的生命。

在“onCreate”结束时添加

    findViewById(R.id.explicacionsLiLa).post(new Runnable(){  //id of your FIRST linearLayout
        public void run(){
            inicialitzarLayout();

        }
    });

有了这个,一旦您的布局膨胀,您将转到将列出视图大小的方法,例如,高度的80%

private void inicialitzarLayour(){
     LinearLayout lilaheight  = findViewById(R.id.explicacionsLiLa).getHeight();

     findViewById(R.id.listview1).getLayoutParams().height = (int)(lilaheight*0.8);

}

PS。如果您没有明确地在帖子中写下来,那么人们不知道您在上一个/上一个/ ..编辑中改变了什么