1

我有 5 个按钮,每个按钮的宽度都不同,因为有些按钮的文本比其他按钮多。

我希望它们对齐,但问题是如果屏幕很小,它们的宽度不适合(在平板电脑上看起来很完美,但在移动设备上看起来一点也不好看)

我怎样才能实现如下图所示的效果?如果屏幕尺寸太小,按钮不适合自动换行,但如果屏幕尺寸足够大,可以让它们全部对齐。

纽扣

布局.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hardwareAccelerated="true"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/button3"
    android:hardwareAccelerated="true" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/id_moduls" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnbmwx6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="BMW X6" />

    <Button
        android:id="@+id/btnbmwz4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btnbmwx6"
        android:text="BMW Z4" />

    <Button
        android:id="@+id/btnbmw1s3d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="BMW 1s 3-Doors" />

    <Button
        android:id="@+id/btnbmw6sgrancoupe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_weight="1"
        android:text="BMW 6s Gran Coupe" />

    <Button
        android:id="@+id/btnbmw5stouring"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="BMW 5s Touring" />
</LinearLayout>

</RelativeLayout>
4

2 回答 2

0

如果你想使用 Android 布局引擎,最好的办法是将 LinearLayout 的weightSum属性设置为 5,并将每个子按钮设置weight为 1 和layout_width“0px”。如果你想要更灵活的多屏幕显示,我为 Android 编写了一个布局引擎,它可以用来极大地改进你处理不同屏幕尺寸的不同视图的方式。该库称为 AbLE,您可以在此处下载。

要使用它,请在您的应用程序目录中创建一个名为libs的文件,然后将该文件下载able.jar到该libs文件夹​​中。接下来,最简单的实现是将您设置Activity为从 ( extend)继承AbLEActivity。接下来,创建一个Buttons.java使用以下代码调用的新类:

@Layout
public class Buttons
{

    public static CharSequence text = "button 1";

    @Layout(viewClass="android.widget.Button")
    public static class Btn1
    {
        public static void onLayoutComplete(AbLEActivity context, View v) 
        {
            int[] size = new int[2];
            AbLEUtil.getScreenSize(context, size);
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = size[0]/5;
            v.setLayoutParams(params);
        }
    }

    @Layout(viewClass="android.widget.Button")
    public static class Btn2
    {
        public static CharSequence text = "button 2";

        public static void onLayoutComplete(AbLEActivity context, View v) 
        {
            int[] size = new int[2];
            AbLEUtil.getScreenSize(context, size);
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = size[0]/5;
            v.setLayoutParams(params);
            v.setX(size[0]*1/5);
        }
    }

    @Layout(viewClass="android.widget.Button")
    public static class Btn3
    {
        public static CharSequence text = "button 3";

        public static void onLayoutComplete(AbLEActivity context, View v) 
        {
            int[] size = new int[2];
            AbLEUtil.getScreenSize(context, size);
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = size[0]/5;
            v.setLayoutParams(params);
            v.setX(size[0]*2/5);
        }
    }

    @Layout(viewClass="android.widget.Button")
    public static class Btn4
    {
        public static CharSequence text = "button 4";

        public static void onLayoutComplete(AbLEActivity context, View v) 
        {
            int[] size = new int[2];
            AbLEUtil.getScreenSize(context, size);
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = size[0]/5;
            v.setLayoutParams(params);
            v.setX(size[0]*3/5);
        }
    }

    @Layout(viewClass="android.widget.Button")
    public static class Btn5
    {
        public static CharSequence text = "button 5";

        public static void onLayoutComplete(AbLEActivity context, View v) 
        {
            int[] size = new int[2];
            AbLEUtil.getScreenSize(context, size);
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = size[0]/5;
            v.setLayoutParams(params);
            v.setX(size[0]*4/5);
        }
    }
}

最后,在您的 中AndroidManifest.xml,将此元数据作为子数据添加到您的<Activity>(即在意图过滤器下方):

<meta-data android:name="layout" android:value="com.yourpackage.android.Buttons" />

您将替换为保存com.yourpackage.android上述Buttons类的包名称的位置。

现在删除setContentView(...)Activity 中的行,然后运行应用程序。您现在应该有一个白色视图,其中的按钮按预期排列。如果您在 XML 文件中有更多布局,您可以通过将此行添加到末尾来轻松地将其添加到布局中Buttons.java

@XMLLayout(resourceID="main")//call this whatever your layout file is (removing the R.layout. part)
public static class InnerXML{}
于 2013-04-29T19:56:37.903 回答
0

您是否使用 wrap_content 为每个按钮指定了宽度?并将它们放在一个水平的LinearLayout中?向我们展示一些代码,这将更容易检查问题

于 2013-04-29T19:36:56.907 回答