在Android中定位按钮

时间:2013-04-29 19:32:07

标签: android button

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

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

我如何实现如下图所示的内容?如果屏幕尺寸太小,则不适合在新线上自动移动的按钮,但是如果屏幕尺寸足够大以使它们全部排成一行。

buttons

layout.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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果您想使用Android版面引擎,最好将LinearLayout的weightSum属性设置为5,并将每个子按钮的weight设置为1,将layout_width设置为“ 0像素”。如果你想为多个屏幕提供更灵活的东西,我为Android编写了一个布局引擎,可以大大改善你处理不同屏幕大小的不同视图的方式。该库名为AbLE,您可以下载它here

要使用它,请在应用程序名为libs的目录中创建一个文件,然后将文件able.jar下载到libs文件夹中。接下来,最简单的实施是您将Activity设置为继承自(extendAbLEActivity。接下来,使用以下代码创建一个名为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类的包名称。{/ p>

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

@XMLLayout(resourceID="main")//call this whatever your layout file is (removing the R.layout. part)
public static class InnerXML{}
相关问题