Подтвердить что ты не робот

Кнопка полуширины экрана

У меня есть одна кнопка в линейном макете. Я хочу, чтобы кнопка занимала половину ширины своего родителя. Есть ли способ сделать это в макете xml.

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:padding="6dp">

    <Button
        android:id="@+id/buttonCollect"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/hello"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"/>

</LinearLayout> 

Мой вопрос очень похож на этот Признать ширину ширины ширины доступной ширины декларативно, за исключением того, что у меня есть только одна кнопка.

4b9b3361

Ответ 1

Да, решение очень похоже на этот вопрос, но вы также хотите установить weightSum родительского LinearLayout:

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    android:padding="6dp"
    android:weightSum="2">

    <Button
        android:id="@+id/buttonCollect"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="@string/hello"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:layout_weight="1" />

</LinearLayout> 

Ответ 2

Вам нужно установить 0,5 (половину) веса и установить ширину в 0.

<Button
    android:id="@+id/buttonCollect"
    android:layout_width="0dip"
    android:layout_weight="0.5"
    android:layout_height="match_parent"
    android:text="@string/hello"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"/>

Ответ 3

Ни один из этих решений не работал у меня на Android L. Мне пришлось сделать wrap_content, как показано ниже:

<Button
    android:layout_gravity="center"
    android:id="@+id/btnSignin"
    android:layout_width="wrap_content"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:layout_height="wrap_content"
    android:text="@string/btn_signin"
    android:layout_marginBottom="@dimen/activity_vertical_margin" />

Ответ 4

Точно так же, как и вопрос, который вы опубликовали, просто используйте вместо этого манекен:

<View 
android:weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>