2

我需要在一个简单的线性布局中设置 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>

我怎样才能纠正最终的错误或做得更好?

这就是我所拥有的:

4

2 回答 2

0

我认为layout_weight在 Button 或 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="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>

此处要更改的主要值是layout_weightListView 和weightSumLinearLayout 的值,但在我看来,在 Button 和 TextView 就位后,您似乎希望它填充视图的其余部分。如果您对上述结果不满意,请使用 layout_weight 和 weightSum。

于 2012-11-12T14:36:20.963 回答
0

一种可能的方法是设置 textview 的高度(例如,我通常设置一个 textSize 然后将高度设置为 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。如果您没有在帖子中明确写下,人们不知道您在上一次/上一次/.. 编辑中更改了什么

于 2012-11-12T15:26:40.707 回答