2013-04-29 87 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{}