2

I need to set the dimension of a listView in a simple Linear Layout. I set the weights as in the example belove, but something went wrong: the textView seems not to be set and the list take a bigger dimension.

<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>

How can I correct the eventual error or do it better?

This is what I have:

double-beep
  • 5,031
  • 17
  • 33
  • 41
Bobo87
  • 151
  • 3
  • 13

2 Answers2

0

I don't think it's a good idea to use layout_weight on the Button, or on the TextView. Now, I'm not quite sure how you want it to look but here's how I would have done it:

<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>

The main values to change here is the layout_weight of the ListView and the weightSum of the LinearLayout, but it seems to me as if you want it to fill the rest of the view, after the Button and TextView are in place. Play around with layout_weight and weightSum if you're not happy with the above result.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • @Bobo87 I have checked your edits now and updated my answer. Please try the edited layout I have above and let me know how that works for you. – Simon Forsberg Nov 12 '12 at 15:40
0

One possible thing is to set the textview's height (for example, i usually set a textSize and then set height as textsize*3.7 )

For example:

   <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" />

if this doesn't help you... the solution could be setting the height programatically.. Maybe it's not the best way, but after maaany problems is what it saved my live many times.

add at the end of "onCreate"

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

        }
    });

With this, once your layout is inflated, you go to the method wich will size your listview, for example, the 80% of the height

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

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

}

PS. People don't know what did you change in your last/previous/.. edits if you don't explicity write it inside your post

Jordi
  • 616
  • 2
  • 9
  • 16