Вопрос

I have a LinearLayout with 2 buttons splitting it horizontally.

I want those buttons to be at the bottom of the screen, so I added to the layout android:layout_weight="bottom" and nothing changes!

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

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

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Start" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Stop" />
    </LinearLayout>

</LinearLayout>
Это было полезно?

Решение

You need to set the layout_width of your buttons to 0dp.

Additionally there is no width property:

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
android:layout_alignParentBottom="true" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Start" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Stop" />
</LinearLayout>

</RelativeLayout>

Другие советы

The layout_weight is used to divide up remaining space after layout_width or layout_height have been taken into account.

When you say

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="Start"
    android:width="0dp" />

The android:layout_width="fill_parent" says to set the size of the button to as wide as its parent. Doing so for two buttons in a horizontal linear layout will result in only one visible button; the second is actually positioned to the right of the first, but offscreen.

If you set the layout_width to 0dp for both buttons, both buttons will have the same width.

If you set the layout_width to wrap_content on both buttons, both will start at their "preferred" width and then have additional space allocated to them based on the ratio of their layout_width to the sum of the layout_widths of all views in the parent layout.

If you want the buttons at the bottom, you can either change your layout to a relative layout (and use alignParentBottom as mentioned in other answers), or nest inside a vertical linear layout (the horizontal layout would have layout_height='wrap_content' and layout_width='fill_parent').

Because you mentioned nesting, you could use something like

<LinearLayout
    andrdoid:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ...
    >
        <LinearLayout
            anddoid:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ...
            >
                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    ... />
                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    ... />
        </LinearLayout>
</LinearLayout>

(or use wrap_content for the layout_width of the buttons to start them at their text widths)

Your title says layout_weight isn't working for you, but the question body implies it is working just fine. The layout_weight attribute should be causing your buttons to take up half the width each.

The positioning at the bottom of the screen is a different issue. Based on your recently edited xml, add android:gravity="bottom" to the parent LinearLayout, or android:layout_gravity="bottom" to the child LinearLayout.

change the layout_width param on the buttons to

android:layout_width="0dp"

Also you can use layout_alignParentBottom

android:layout_alignParentBottom="true"

You can create your layout file like following

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

  <!-- Other Components of your layout file -->
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom"
  android:layout_alignParentBottom="true"
  android:orientation="horizontal"
  android:weightSum="2">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Start"/>

    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Stop"/>
  </LinearLayout>
</RelativeLayout>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top