开发者

Make a component fill all available space on screen

I want something like this:

Make a component fill all available space on screen

But I don't know how to resize the TextView so it gets all the available space on screen which is not ocuppied by the EditText or the Buttons. May I do it in the code, or in the xml?

At XML I tried putting the TextView into a FrameLayout, but it makes no differenc开发者_如何学Goe. Currently looks like:

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

    <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/consola"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:scrollbars = "vertical"
            android:text="@string/hello"/>

    </FrameLayout>

    <EditText 
        android:id="@+id/comando"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

        <Button
            android:text="Conectar"
            android:id="@+id/boton_conectar"
            android:layout_width="wrap_content"
            android:layout_weight="0.5"
            android:layout_height="wrap_content">
        </Button>
        <Button
            android:text="Enviar"
            android:id="@+id/boton_enviar"
            android:layout_width="wrap_content"
            android:layout_weight="0.5"
            android:layout_height="wrap_content">
        </Button>
    </LinearLayout>
</LinearLayout>

At the code I'm just checking if the Buttons are pushed with Listeners. One of them, when pushed, gets the text at the EditText, and appends it to the TextView. It works, and TextView gets higher, while EditText and Buttons downs one line. If I go on appending lines, finaly EditText and Buttons get out of the screen. I want to avoid this behaviour, and accomplish to get this 3 widgets sticked to the bottom of the screen.


Use the android:layout_weight=1 attribute, like the buttons on the bottom of the form. That will assign most of the space to it and anything that's left to the rest of the elements.


It's all about the weight. This should give you what you want:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="TextView"
        android:id="@+id/textView1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </TextView>
    <EditText android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <requestFocus></requestFocus>
    </EditText>
    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button android:text="Button"
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_width="0dp">
        </Button>
        <Button android:text="Button"
            android:id="@+id/button2"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1">
        </Button>
    </LinearLayout>
</LinearLayout>

(Side note: When using weight, setting the corresponding height/width to 0dp sometimes gets around some weird behavior.)


Try android:fillViewport="true".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜