【问题标题】:How to make all buttons to be of the same height (and keep their weigths)?如何使所有按钮具有相同的高度(并保持它们的重量)?
【发布时间】:2013-05-07 20:40:12
【问题描述】:

<LinearLayout 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:orientation="horizontal"
    android:weightSum="6"
    tools:context=".MainActivity" >

    <Button 
        android:text="BTN1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />
    <Button 
        android:text="BTN2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical"
        android:layout_weight="2" />
    <Button 
        android:text="BTN3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />
    <Button 
        android:text="BTN4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />
    <Button 
        android:text="BTN5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />

</LinearLayout>

使用android:layout_height="match_parent" 只是将按钮拉伸到整个屏幕高度。我希望按钮 2 的宽度是其他按钮的两倍。

【问题讨论】:

  • 减小文本大小 android:textSize="10dp"。减小所有按钮的文本大小
  • @Raghunandan 这不是一个可接受的解决方案。我想保持一切原样,文本大小、颜色、重量、按钮的顺序,只是按钮 2,在这种情况下,我希望它与同一行上其他按钮的高度相匹配
  • 那么它认为不可能。好吧,我很好奇,期待更好的解决方案
  • @Raghunandan 查看已接受的答案 + 我的评论
  • @Raghunandan 这就是这个问题的重点,我不希望文本在同一行。我希望按钮的高度相同,无论它们显示多少行文本。

标签: android android-layout button height


【解决方案1】:

需要将LinearLayout高度改为wrap_content,然后将所有高度改为match_parent。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="6"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:text="BTN1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />
    <Button 
        android:id="@+id/button2"
        android:text="BTN2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="2" />
    <Button 
        android:id="@+id/button3"
        android:text="BTN3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />
    <Button 
        android:id="@+id/button4"
        android:text="BTN4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />
    <Button 
        android:id="@+id/button5"
        android:text="BTN5"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1" />

</LinearLayout>

【讨论】:

  • 刚刚测试过,结果一样。
  • 抱歉,您似乎不知道您在说什么。如果您阅读了该问题,您会发现我确实尝试过match_parent,但它不起作用。如果您在发布之前尝试您的建议会很好,除非您完全确定解决方案并且还可以解释为什么它解决了它所解决的问题。
  • 我在发布之前在设备上尝试了更新的解决方案。如果您将 LinearLayout 的 layout_height 更改为 wrap_content,则 match_parent 将起作用。
  • 现在你是对的,但是你忘了在你的代码中改变它。这是 Archie.bpgc 接受的解决方案
【解决方案2】:

你已经做对了。第二个按钮的宽度已经是其余按钮的两倍。

剩下的问题是高度。高度的差异是因为按钮内的文本对于这么小的按钮来说太大了。只需减小文本大小就可以了。

【讨论】:

  • 按照@Archie.bpgc 的建议,即使填充垂直空间并使第二个按钮“匹配父级”也能产生奇迹
【解决方案3】:

这样就可以了:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="6"
    tools:context=".MainActivity" >

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

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="2"
        android:text="BTN2" />

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

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

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

</LinearLayout>

使父布局高度wrap_content和第二个按钮的高度match_parent

【讨论】:

  • 这几乎是解决方案。原因是在这里我放了一个带有按钮文本“BTNx”的示例。在现实生活中,按钮的文本事先并不知道。我尝试了将父 LinearLayout 高度设置为wrap_content 的解决方案,但所有按钮(而不仅仅是“BTN2”)高度设置为match_parent,它工作正常,这对我来说是完整的解决方案。谢谢。
  • 如果按钮的文本是三行,这不起作用。为什么?
【解决方案4】:

给你...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="6"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="fill_vertical"
        android:layout_toLeftOf="@+id/Button2"
        android:layout_weight="1"
        android:text="BTN1as s dh asgdjhsg djahgsd  gsadhasdhg sa" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Button1"
        android:layout_alignParentTop="true"
        android:layout_gravity="fill_vertical"
        android:layout_toLeftOf="@+id/Button3"
        android:layout_weight="1"
        android:text="BTN2" />

    <Button
        android:id="@+id/Button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Button1"
        android:layout_alignParentTop="true"
        android:layout_gravity="fill_vertical"
        android:layout_toLeftOf="@+id/Button4"
        android:layout_weight="1"
        android:text="BTN3" />

    <Button
        android:id="@+id/Button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Button1"
        android:layout_alignParentTop="true"
        android:layout_gravity="fill_vertical"
        android:layout_toLeftOf="@+id/Button5"
        android:layout_weight="1"
        android:text="BTN4" />

    <Button
        android:id="@+id/Button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Button1"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1"
        android:text="BTN5" />

</RelativeLayout>

【讨论】:

  • 您更改了按钮 2 的权重。我希望能够使用宽度权重。
猜你喜欢
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
相关资源
最近更新 更多