与sqare按钮的Andr​​oid布局布局、按钮、sqare、oid

2023-09-12 06:21:26 作者:笑容下的凄凉

我想要一个与此相似的布局:

四方形按钮在屏幕上 - 每个那些使用一半/屏幕高度屏幕(取smaler)的。独立的屏幕尺寸/分辨率。

我已经尝试过使用来实现这一的LinearLayout 但按键都结束了使用正确的宽度,但仍然有背景的高度(非方形更多)。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< LinearLayout中的android:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>
    <按钮机器人:layout_height =WRAP_CONTENT的风格=@风格/ CKMainButton
        机器人:layout_width =FILL_PARENT机器人:文本=@字符串/景点
        机器人:ID =@ + ID / ApplicationMainSight机器人:layout_toLeftOf =@ + ID / ApplicationMainEvent>< /按钮>

    <按钮机器人:layout_height =WRAP_CONTENT的风格=@风格/ CKMainButton
        机器人:layout_width =FILL_PARENT机器人:文本=@字符串/景点
        机器人:ID =@ + ID / ApplicationMainSight机器人:layout_toLeftOf =@ + ID / ApplicationMainEvent>< /按钮>
< / LinearLayout中>

< LinearLayout中的android:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>
    <按钮机器人:layout_height =WRAP_CONTENT的风格=@风格/ CKMainButton
        机器人:layout_weight =1机器人:layout_width =FILL_PARENT
        机器人:文本=@字符串/ usergenerated机器人:ID =@ + ID / ApplicationMainUserGenerated>< /按钮>
    <按钮机器人:layout_height =WRAP_CONTENT的风格=@风格/ CKMainButton
        机器人:layout_weight =1机器人:layout_width =FILL_PARENT
        机器人:文本=@字符串/游机器人:ID =@ + ID / ApplicationMainTour>< /按钮>
< / LinearLayout中>
< / LinearLayout中>
 

它看起来像这样:

我怎样才能acchieve布局看起来像图像在上面的顶部?

解决方案

进口android.content.Context; 进口android.util.AttributeSet; 进口android.widget.LinearLayout; 公共类SquareLayout扩展的LinearLayout {     //合适的宽度与高度比 - 1.0万     私人最终双mScale = 1.0;     公共SquareLayout(上下文的背景下){         超(上下文);     }     公共SquareLayout(上下文的背景下,ATTRS的AttributeSet){         超(背景下,ATTRS);     }     @覆盖     保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){         INT宽度= MeasureSpec.getSize(widthMeasureSpec);         INT高= MeasureSpec.getSize(heightMeasureSpec);         如果(宽>(INT)((mScale *高)+ 0.5)){             宽度=(INT)((mScale *高)+ 0.5);         } 其他 {             高度=(int)的((宽度/ mScale)+0.5);         }         super.onMeasure(                 MeasureSpec.makeMeasureSpec(宽,MeasureSpec.EXACTLY)                 MeasureSpec.makeMeasureSpec(高度,MeasureSpec.EXACTLY)         );     } }

I want to make a layout similar to this one:

Four square buttons on the screen - each of those using half of the screen with/screen height (whichever is smaler). Independent of screen size/resolution.

I already tried to achieve this by using a LinearLayoutbut the buttons are ending up using the correct width, but still having the height of the background (not square any more).

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

It's looking like this:

How can i acchieve the Layout to look like the image at the top above?

解决方案

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)
        );
    }
}