Android 等宽按钮

Android 等宽按钮,android,android-layout,Android,Android Layout,我有两个按钮在一个垂直的线性布局。是否有可能使它们具有相同的宽度,理想情况下与最宽的文本一样宽 <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_

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

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

使用以下代码段:

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

使用layout_alignRight和layout_DownlightLeft:

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

使用下面的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_内容,因此按钮将根据内容占用空间。因此,如果您希望两个按钮的长度相同,则必须为按钮的宽度指定填充宽度。

问了这个问题后,我发现我可以使用表格布局而不是线性布局来获得所需的效果。

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

下面我使用了权重,它将分配总权重,您可以将总权重设置到每个元素中,我将展示它将如何占据您刚才参考我的设计图片的位置

您可以使用布局权重进行此操作。就像下面这样使用

    <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"
   />
用这个。。。


尝试使用布局重量?@SachinG是的。因为LinearLayout的方向设置为垂直,所以布局的权重适用于孩子的身高。@我不知道你为什么否决了这个问题。这个问题听起来不错。我编辑了我的答案,你希望看到的是,只有当按钮的父项是亲戚时,这才起作用。这没有回答问题。我需要两个相同的按钮,里面有一个垂直方向的线性布局。在上面我使用了水平的概念,你可以替换垂直的,就是这样,你可以得到你的答案。我不知道你的解决方案将如何给出我要找的答案。您能给出一个只有两个按钮的简单示例吗?因为LinearLayout的方向是垂直的,所以权重应用于每个子项的高度,而不是宽度。@Code Guru我刚刚复制了您的代码并进行了所需的更改。您已将方向垂直放置。您正在提问或只是检查人们的技术知识。您是否测试了所需的更改以检查它们是否对按钮的宽度有影响?我在问一个问题,并说明您提出的解决方案不起作用,以及原因。不适合理想情况下的按钮宽度最广泛的文本标准。这将使按钮的宽度与父级按钮的宽度相同。@通过将父级按钮的宽度设置为wrap_content,SachinG一个小的调整可以解决这一问题。请解释您的代码如何回答我的问题。另外,所有这些XML真的有必要回答这个问题吗?我认为这比必要的要复杂得多。为什么每个按钮都在线性布局中?
    <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"
   />
<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>
<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>