如何在Android中为按钮保持固定的宽高比 [英] How to keep fixed aspect ratio for a button in android

查看:46
本文介绍了如何在Android中为按钮保持固定的宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a set of buttons in a horizontal layout. I have set an image drawable as the background for each of these buttons. But when I make the linear layout to span the width of the screen, the buttons lose their aspect ratio. I want to keep its aspect ratio as is. I mean I have to keep it square shaped through out the application what ever the screen resolution is. How can I do this. Any suggestions is appreciated.

这是我的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.我没有测试过,但是应该可以.建议的更改是将图像按钮设置为src,而不是将图像设置为背景.同样,如果您坚持使用Button,则可以将每个按钮包装为另一种布局,并将其gravity属性设置为"center".看看下面的XML.

I'm giving an improvement to your XML. I have not tested it, but it should work. The suggested changes are, instead of setting the image as background, set it as src for ImageButtons. Also, if you insists the Button, you can wrap each of the buttons with another layout and set its gravity property to "center". Take a look at the following 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中为按钮保持固定的宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆