【问题标题】:How to keep fixed aspect ratio for a button in android如何在android中保持按钮的固定纵横比
【发布时间】:2012-01-31 15:01:40
【问题描述】:

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

这是我的 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" ?
  • 是的,我尝试删除它。但是没有用。

标签: android android-layout


【解决方案1】:

我正在改进您的 XML。我没有测试过它,但它应该可以工作。建议的更改不是将图像设置为背景,而是将其设置为 ImageButtons 的 src。此外,如果您坚持使用 Button,则可以使用另一个布局包装每个按钮并将其重力属性设置为“中心”。看看下面的 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 参数是什么意思吗?
  • 这只是意味着与其他布局相比,布局可以跨越最大可用大小。假设如果有两个 android:layout_weight="1" 的布局,它们将平等地共享屏幕。在这里阅读更多:developer.android.com/reference/android/widget/…
  • 谢谢!设置 src 属性而不是 background 会有所不同。
【解决方案2】:

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

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

【讨论】:

  • 但是,我无法设置布局的大小。意味着,我让它们在更大的结果上进行扩展。
  • 不要考虑缩放,weight=1 表示它会根据屏幕尺寸设置尺寸。首先检查建议/答案,然后您就会对此有所了解。
【解决方案3】:

这里我使用权重进行布局,你可以试试这样。为线性布局设置权重,然后根据需要将按钮的宽度设置为 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>

【讨论】:

    【解决方案4】:
    <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,防止不必要的按钮拉伸。

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 2011-06-07
      • 1970-01-01
      • 2015-12-02
      • 2013-07-07
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      相关资源
      最近更新 更多