如何在安卓中保持按钮的固定宽高比

7
我有一组水平布局的按钮。我已经为每个按钮设置了一个图像可绘制的背景。但是当我将线性布局扩展到屏幕宽度时,按钮失去了它们的纵横比。我想保持它的纵横比不变。我的意思是,无论屏幕分辨率如何,我都必须保持它的正方形形状在整个应用程序中。我该怎么做?欢迎任何建议。
这是我的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>

谢谢你


你首先尝试移除android:height="50dip"属性了吗? - C--
是的,我尝试过删除它。但是没有起作用。 - Rony Joy
4个回答

3
我正在改进您的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>

如果不起作用,请随时询问。


好的,它运行了。但是还有一些问题。当我移除了50dip参数后,它正常工作了。你能告诉我android:layout_weight参数的含义吗? - Rony Joy
这仅仅意味着布局可以跨越与其他布局相比的最大可用尺寸。例如,如果有两个布局都有android:layout_weight =“1”,它们将平均分配屏幕。在此处阅读更多信息:http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html - C--
谢谢!将src属性设置而不是background使得区别明显。 - Trent

0
<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,避免了按钮不必要的拉伸。


0

在这里,我使用权重来布局,您可以尝试这样做。为线性布局设置权重总和,然后将按钮的宽度设置为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>

0

如果您想要精确的正方形,那么宽度和高度必须设置为相同的值。

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


但是,我无法为布局设置大小。这意味着,在更大的分辨率上,它们会按比例缩放。 - Rony Joy
不要考虑缩放,weight=1 表示它将根据屏幕尺寸设置大小。首先检查建议/答案,然后您就会对此有一个想法。 - Yugandhar Babu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接