2013-04-29 90 views
1

我有5个按钮,每个按钮的宽度都不相同,因为某些按钮的文字比其他文字多。Android中的定位按钮

我希望他们一致,但问题是,他们不适合在宽屏幕显示为小(在平板电脑看起来很完美,但在移动设备上不都不好看)

我如何能实现类似于下面的图片?如果屏幕尺寸太小,那些不适合自动换行的按钮,但是屏幕尺寸足够大,可以使它们全部排成一行。

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> 

回答

0

你给每个按钮与WRAP_CONTENT的宽度?并把它们放在一个水平的LinearLayout中? 向我们展示一些代码,这将使如果你想使用Android布局引擎更容易检查问题

+0

我增加了布局。 – 2013-04-29 19:54:31

0

,最好的办法是到的LinearLayout的weightSum属性设置为5,并设置每个子按钮的weight 1和layout_width为“0px”。如果您希望为多个屏幕提供更灵活的功能,我为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类的包名称。

现在删除活动中的行setContentView(...),然后运行应用程序。您现在应该有一个白色的视图,按钮按预期排列。如果你在一个XML文件有更多的布局,你可以很容易地通过这个加入这一行到Buttons.java年底添加到您的布局:

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