带有方形按钮的 Android 布局

标签 android xml xslt layout

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

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

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

最佳答案

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

关于带有方形按钮的 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2948212/

相关文章:

android - 无法从 Firebase gcloud 命令行启动仪器测试

IE 中的 Javascript 权限被拒绝错误,创建 xml 文件时在 firefox 中未定义 ActiveXObject

html - 我可以直接在 html 页面中进行 XSLT 转换吗?

c# - Xamarin Android MvvmCross 绑定(bind) facebook 登录按钮

android - 我如何区分它是 FireStick 还是 FireTV

xml - 用于收集属性作为每个选定节点的名称/值映射的 XPath 3.1 表达式

xslt - 如何检查XSLT中是否存在标签?

.net - 使用 xslt 客户端拆分字符串

android - 将 facebook 和 google 集成到同一个应用程序中,并允许用户同时登录

java - 解析可用对象的奇怪 xml 响应