繁体   English   中英

如何在 android 中保持按钮的固定纵横比

[英]How to keep fixed aspect ratio for a button in android

我有一组水平布局的按钮。 我已将可绘制图像设置为每个按钮的背景。 但是当我使线性布局跨越屏幕的宽度时,按钮失去了它们的纵横比。 我想保持其纵横比不变。 我的意思是无论屏幕分辨率如何,我都必须在整个应用程序中保持方形。 我怎样才能做到这一点。 任何建议表示赞赏。

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_background"
    android:baselineAligned="false"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/answer_bg"
        android:padding="10dip" >
            <Button
                android:id="@+id/b2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/img_background"
                android:height="50dip"
                android:text="@string/x"
                android:textStyle="bold" />
            <Button
                android:id="@+id/b3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/img_background"
                android:height="50dip"
                android:text="@string/x"
                android:textStyle="bold" />
            <Button
                android:id="@+id/b4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/img_background"
                android:height="50dip"
                android:text="@string/x"
                android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

谢谢

我正在改进你的XML。 我没有测试它,但它应该工作。 建议的更改是,而不是将图像设置为背景,将其设置为ImageButtons的src。 此外,如果您坚持使用Button,则可以使用其他布局包装每个按钮,并将其重力属性设置为“center”。 看一下下面的XML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_background"
    android:baselineAligned="false"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/answer_bg"
        android:padding="10dip" >
<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" >
            <Button
                android:id="@+id/b2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/img_background"
                android:height="50dip"
                android:text="@string/x"
                android:textStyle="bold" /></LinearLayout>
<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" >
            <Button
                android:id="@+id/b3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/img_background"
                android:height="50dip"
                android:text="@string/x"
                android:textStyle="bold" /></LinearLayout>
<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" >
            <Button
                android:id="@+id/b4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/img_background"
                android:height="50dip"
                android:text="@string/x"
                android:textStyle="bold" /></LinearLayout>
    </LinearLayout>
</LinearLayout>

随意询问它是否不起作用。

在这里我使用重量布局你可以尝试这样。线性布局的重量,然后根据您的需要将按钮的宽度设置为0dp和重量。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_background"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/answer_bg"
        android:padding="10dip"
        android:weightSum="1" >

        <Button
            android:id="@+id/b2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".33"
            android:background="@drawable/img_background"
            android:text="@string/x"
            android:textStyle="bold" />

        <Button
            android:id="@+id/b3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".33"
            android:background="@drawable/img_background"
            android:height="50dip"
            android:text="@string/x"
            android:textStyle="bold" />

        <Button
            android:id="@+id/b4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".33"
            android:background="@drawable/img_background"
            android:text="@string/x"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>
<ImageButton
                android:layout_marginBottom="4dp"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="2dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:adjustViewBounds="true"
                android:background="@color/white"
                android:scaleType="fitCenter"
                android:src="@mipmap/all"></ImageButton>

刻度类型 fitCenter 包括按钮内的完整图像。 3 个图像按钮嵌套在水平线性布局中,每个按钮的权重都为 1。 使 adjustViewbounds= true,防止不必要的按钮拉伸。

如果您想要精确的方形意味着您必须为相同的值设置宽度和高度。

如果要在水平布局中使用相同形状的所有3个按钮,而不考虑屏幕分辨率,请使用layout_weight = 1 根据我对布局的了解,只有这是解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM