【问题标题】:Android layout with square buttons带有方形按钮的 Android 布局
【发布时间】:2011-02-26 06:04:49
【问题描述】:

我想做一个类似这样的布局:

www.ImageBanana.net - layout.png http://www.imagebanana.com/img/9kmlhy66/thumb/layout.png

屏幕上有四个方形按钮 - 每个按钮都使用屏幕高度的一半(以较小者为准)。与屏幕尺寸/分辨率无关。

我已经尝试通过使用 LinearLayout 来实现这一点,但按钮最终使用了正确的宽度,但仍然具有背景的高度(不再是正方形)。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button 
            android:layout_height="wrap_content" 
            style="@style/CKMainButton"
            android:layout_width="fill_parent" 
            android:text="@string/sights"
            android:id="@+id/ApplicationMainSight" 
            android:layout_toLeftOf="@+id/ApplicationMainEvent"></Button>

        <Button 
            android:layout_height="wrap_content" 
            style="@style/CKMainButton"
            android:layout_width="fill_parent" 
            android:text="@string/sights"
            android:id="@+id/ApplicationMainSight" 
            android:layout_toLeftOf="@+id/ApplicationMainEvent"></Button>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button 
            android:layout_height="wrap_content" 
            style="@style/CKMainButton"
            android:layout_weight="1" 
            android:layout_width="fill_parent"
            android:text="@string/usergenerated" 
            android:id="@+id/ApplicationMainUserGenerated" />

        <Button 
            android:layout_height="wrap_content" 
            style="@style/CKMainButton"
            android:layout_weight="1" 
            android:layout_width="fill_parent"
            android:text="@string/tours" 
            android:id="@+id/ApplicationMainTour"/>

    </LinearLayout>
</LinearLayout>

看起来像这样:www.ImageBanana.net - layout2.png http://www.imagebanana.com/img/i2ni6g4/thumb/layout2.png

我怎样才能使布局看起来像上面的图像?

【问题讨论】:

    标签: android xml xslt layout


    【解决方案1】:

    好的,这是一个修改后的例子:

    <?xml version="1.0" encoding="utf-8"?>
    <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent" android:layout_width="fill_parent">
        <RelativeLayout android:id="@+id/magaLoginLayout"
            android:layout_height="fill_parent" android:layout_width="fill_parent">
            <Button android:text="@+id/Button01" android:id="@+id/Button01"
                android:layout_width="160dip" android:layout_height="160dip" android:layout_marginTop="20dip"></Button>
            <Button android:text="@+id/Button03" android:layout_below="@+id/Button01" android:id="@+id/Button03"
                android:layout_alignLeft="@+id/Button01" android:layout_height="160dip" android:layout_width="160dip"></Button>
            <Button android:text="@+id/Button04" android:layout_below="@+id/Button01" android:id="@+id/Button04"
                android:layout_toRightOf="@+id/Button03" android:layout_height="160dip" android:layout_width="160dip"></Button>
            <Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_width="wrap_content"
                android:layout_toRightOf="@+id/Button01" android:layout_alignTop="@+id/Button01" android:layout_alignParentRight="true" android:layout_height="160dip"></Button>
    
    
    </RelativeLayout>
    
    </ViewFlipper>
    

    【讨论】:

    • 我想你误解了我的意思——我的目标不是对齐(它已经在工作),而是按钮的缩放。我希望每行两个按钮都与屏幕一样宽,所以你的答案不是正确的。对不起。
    • 抱歉,将 with 设置为绝对尺寸对我不起作用 - 它必须与屏幕尺寸/方向无关
    • 160dip 硬编码大小是一个错误,因为存在一些设备,例如 Galaxy Note。
    【解决方案2】:
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.LinearLayout;
    
    public class SquareLayout extends LinearLayout {
        // Desired width-to-height ratio - 1.0 for square
        private final double mScale = 1.0;
    
        public SquareLayout(Context context) {
            super(context);
        }
    
        public SquareLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
    
            if (width > (int)((mScale * height) + 0.5)) {
                width = (int)((mScale * height) + 0.5);
            } else {
                height = (int)((width / mScale) + 0.5);
            }
    
            super.onMeasure(
                    MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
            );
        }
    }
    

    【讨论】:

    • 您的示例中的mScale 是什么?
    • android:layout_width|height 应设置为 match_parent´, sister layouts/views should be wrap_content`。
    • @Mannaz 和其他后来找到答案的人:mScale 是您想要的宽高比,使用 1 表示正方形
    • 只是想说这个效果很好,我已经把它添加到我的技巧包中了。
    • 在onMeasure中,高度和宽度可能为0,所以这不是一个完美的解决方案
    【解决方案3】:

    我的解决方案与第一个类似,但是,我在 onLayout 方法中调整大小。不知道哪个更好。

    为方便起见,我将我的解决方案封装在一个不错的 Android 库中,请参阅 https://github.com/ronaldsteen/Android-SquareLayout-Library

    【讨论】:

    • 你应该在 onMeasure() 中计算你的最终尺寸。如果框架不喜欢您选择的内容,它将再次调用 onMeasure(),并将 measurespecs 设置为 EXACTLY。
    【解决方案4】:

    我通常会这样做:

    View v; // the view i want to layout squared
    v.measure(1,1);
    int size = v.getMeasuredHeight(); // or v.getMeasuredWidth()...
    LayoutParams params = ... // either your own, or returned from View
    params.width = size;
    params.height = size;
    v.setLayoutParams(params);
    

    这当然不是最复杂的代码,因为它保留了可以在 MeasureSpec 中利用的大多数功能,但它完成了工作,因为 'v.measure(1,1)' 只是说“衡量我的观点是!”。

    我的方法的绝对优势是您不必对任何东西进行子类化,也不必在您的ViewTreeObserver 中添加一些OnGlobalLayoutListener。这一切都适用于您在代码中构建或扩展布局的地方。

    【讨论】:

      【解决方案5】:

      我推荐以下方式,它简单且有效

      public class SquareLayout extends RelativeLayout {
      
         public SquareLayout(Context context) {
             super(context);
         }
      
         public SquareLayout(Context context, AttributeSet attrs) {
             super(context, attrs);
         }
      
         @Override
         public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
             super.onMeasure(widthMeasureSpec, widthMeasureSpec);
         }
      
      }
      

      【讨论】:

      【解决方案6】:

      您可以使用ConstraintLayout 实现此目的。您应该只添加具有 50% 屏幕宽度位置的垂直指南,将您的按钮与指南的左侧和右侧对齐,并以 1:1 的纵横比调整它们的大小。一切都可以在布局 XML 文件中完成,无需额外的源代码。

      完成所有这些步骤后,您将得到如下结果:

      <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <Button
              android:id="@+id/button1"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:background="@android:color/holo_orange_light"
              android:text="1"
              app:layout_constraintDimensionRatio="H,1:1"
              app:layout_constraintEnd_toStartOf="@+id/guideline"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <Button
              android:id="@+id/button2"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:background="@android:color/holo_blue_light"
              android:text="2"
              app:layout_constraintDimensionRatio="W,1:1"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toEndOf="@+id/guideline"
              app:layout_constraintTop_toTopOf="parent" />
      
          <Button
              android:id="@+id/button3"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:background="@android:color/holo_green_dark"
              android:text="3"
              app:layout_constraintDimensionRatio="W,1:1"
              app:layout_constraintEnd_toStartOf="@+id/guideline"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/button1" />
      
          <Button
              android:id="@+id/button4"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:background="@android:color/holo_purple"
              android:text="4"
              app:layout_constraintDimensionRatio="W,1:1"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toEndOf="@+id/guideline"
              app:layout_constraintTop_toBottomOf="@+id/button2" />
      
          <android.support.constraint.Guideline
              android:id="@+id/guideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              app:layout_constraintGuide_percent="0.5" />
      
      </android.support.constraint.ConstraintLayout>
      

      以下是此布局在最小和最大屏幕上的外观:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        • 1970-01-01
        • 2013-08-06
        • 2012-09-15
        • 1970-01-01
        相关资源
        最近更新 更多