我有一组水平布局的按钮。我已将可绘制图像设置为每个按钮的背景。但是当我使线性布局跨越屏幕的宽度时,按钮失去了它们的纵横比。我想保持其纵横比不变。我的意思是无论屏幕分辨率如何,我都必须在整个应用程序中保持方形。我怎样才能做到这一点。任何建议表示赞赏。
这是我的 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,则可以用另一个布局包裹每个按钮,并将其 gravity 属性设置为“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>
如果它不起作用,请随时询问。
关于android - 如何在android中保持按钮的固定纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9081747/