【问题标题】:how to make buttons of the same size如何制作相同大小的按钮
【发布时间】:2016-11-24 01:20:08
【问题描述】:

这是我的 xml,这是结果,但希望上面的 3 个按钮(一起)占据最大按钮的相同大小,比如那个图像,我应该在哪里更改我的布局?

这就是我所拥有的:

这就是我想要的:

我的 xml:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#FFA500"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="#FFA500"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textoPoup1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
                android:textColor="@android:color/white"/>

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

                <Button
                    android:id="@+id/nota0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"/>

                <Button
                    android:id="@+id/nota40"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="50"/>

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


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

                <Button
                    android:id="@+id/nota120"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="110"/>

                <Button
                    android:id="@+id/nota160"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="170"/>

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

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

【问题讨论】:

  • 使用 layout_weight 试试
  • 每个小按钮应该是这样的:
  • 嵌套布局是一种反模式,因为它不利于性能。
  • @BobMalooga 我一直想知道如何在没有嵌套的情况下获得所需的外观(我已经非常习惯于使用 LinearLayouts,主要是偶尔使用 Frame/Relative)?
  • @BobMalooga 哇...只是。哇。男孩,你让我开心。谢谢老哥!

标签: android xml android-layout android-studio layout


【解决方案1】:

这可以通过使用layout_weight 为每个按钮轻松完成,如下所示:

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

这样,每个按钮将占据其父级宽度的 1/3,这将占据整个屏幕宽度。您仍然可以使用边距和内边距来调整按钮之间的间距。

编辑

使用chains 并应用app:layout_constraintHorizontal_weight="1" 之类的水平或垂直权重,您可以更轻松地使用ConstraintLayout 做同样的事情而无需嵌套

【讨论】:

    【解决方案2】:

    在带有 3 个按钮的 LinearLayout 中添加

    android:orientation="horizontal"
    

    现在,为所有三个按钮设置这个:

    android:layout_width="0dp"
    android:layout_weight="1"
    

    类似这样的:

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <Button
                    android:id="@+id/nota0"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="0"/>
    
                <Button
                    android:id="@+id/nota40"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="50"/>
    
                <Button
                    android:id="@+id/nota80"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="90"/>
            </LinearLayout>
    

    然后你可以添加适当的边距:

    对于第一个按钮,仅将 leftMargin 添加为 xdp,将 rightMargin 添加为 x/2dp。 对于第二个按钮,将左右边距添加为 xdp。 对于第三个按钮,添加左边距为 x/2dp,右边距为 xdp。

    【讨论】:

      【解决方案3】:

      在linearlayout中,我们可以给出layout_weight属性,用于在linearlayout中为每个子元素的大小加权,如果layout_width="0dp"它将根据权重计算该元素的宽度,或者如果layout_height="0dp"它将根据重量计算该元素的高度。

      它取总元素的平均权重,即如果所有元素的 layout_weight="1" 它将分配其父元素宽度的 1/3。

      因此,在您的情况下,您希望按钮大小相同,因此需要为所有元素和 layout_width="0dp" 分配相同的权重

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:orientation="vertical">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="10dp"
          android:background="#FFA500"
          android:orientation="vertical">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_margin="10dp"
              android:background="#FFA500"
              android:orientation="vertical">
      
              <TextView
                  android:id="@+id/textoPoup1"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:text="1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
                  android:textColor="@android:color/white"/>
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
                  <Button
                      android:id="@+id/nota0"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:text="0"/>
      
                  <Button
                      android:id="@+id/nota40"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:text="50"/>
      
                  <Button
                      android:id="@+id/nota80"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:text="90"/>
              </LinearLayout>
      
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
                  <Button
                      android:id="@+id/nota120"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:text="110"/>
      
                  <Button
                      android:id="@+id/nota160"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:text="170"/>
      
                  <Button
                      android:id="@+id/nota200"
                      android:layout_width="0dp"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:text="300"/>
              </LinearLayout>
      
              <Button
                  android:id="@+id/proxima1"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="Next"/>
          </LinearLayout>
      </LinearLayout>
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2015-09-28
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        • 1970-01-01
        • 2011-02-12
        • 2017-09-21
        相关资源
        最近更新 更多