等宽的按钮

时间:2014-04-19 05:30:16

标签: android android-layout

我在垂直LinearLayout中有两个按钮。是否有可能使它们具有相同的宽度,理想情况下与最宽的文本一样宽?

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="codeguru.swiss.MainFragment">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <Button
            android:id="@+id/newTournament"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/newTournament" />

        <Button
            android:id="@+id/loadTournament"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loadTournament" />
    </LinearLayout>


</RelativeLayout>

8 个答案:

答案 0 :(得分:1)

使用以下xml代码

<LinearLayout
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <Button
        android:id="@+id/newTournament"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/newTournament" />

    <Button
        android:id="@+id/loadTournament"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/loadTournament" />
</LinearLayout>

在您的代码中,您正在使用wrap_content,因此按钮将根据内容占用空间。因此,如果您希望两个按钮的长度相同,则必须使用按钮宽度来填充父级宽度。

答案 1 :(得分:1)

在提出这个问题之后,我发现我可以使用TableLayout代替LinearLayout来获得所需效果。

答案 2 :(得分:1)

用这个......

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <Button
        android:id="@+id/newTournament"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="kkkkkkkkkkkkkkk" />

    <Button
        android:id="@+id/loadTournament"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/loadTournament" />
</LinearLayout>

答案 3 :(得分:0)

使用以下代码段:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >

     <Button
    android:id="@+id/newTournament"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/newTournament" />
</LinearLayout>
 <LinearLayout 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >

      <Button
    android:id="@+id/loadTournament"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/loadTournament" />
     </LinearLayout>

答案 4 :(得分:0)

您可以像这样均匀地分配布局宽度......

下面我使用了它将分配总重量的重量,你可以设置每个元素的总重量,我会展示你将如何参考我的设计图片的地方

enter image description here

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />
</LinearLayout>

答案 5 :(得分:-1)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MyActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <Button
                android:id="@+id/btnEnable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Enable"/>

            <Button
                android:id="@+id/btnDisable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Disable"/>

            <Button
                android:id="@+id/btnLock"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Lock"/>

            <Button
                android:id="@+id/btnSetPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Set Password"/>

        </LinearLayout>

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

    </LinearLayout>

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

        <Button
            android:id="@+id/btnWipeData"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Wipe Data"/>

        <android.support.v7.widget.AppCompatCheckBox
            android:id="@+id/chkWipeExtStorage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Also Wipe External Storage"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <Button
                android:id="@+id/btnClearPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Clear Password" />

            <Button
                android:id="@+id/btnLockTask"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Lock Task" />

        </LinearLayout>

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

答案 6 :(得分:-3)

使用layout_alignRight和layout_alightLeft:

<Button
            android:id="@+id/loadTournament"
            android:layout_alignRight="@+id/newTournament"
            android:layout_alignLeft="@+id/newTournament"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loadTournament" />

答案 7 :(得分:-3)

您可以使用layout_weight。只需使用如下,

    <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
>

<Button
    android:id="@+id/newTournament"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/newTournament"
    />

<Button
    android:id="@+id/loadTournament"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/loadTournament"
   />

相关问题