2012-04-22 85 views
0

可能重複:
Button half width of screen按鈕與Android中屏幕的一半寬度

我想創建一個線性層(水平方向)內的兩個按鈕,其具有半每一個屏幕寬度,還有一點餘量。 我有這樣的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/main" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/txtTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:paddingTop="5dp" 
      android:text="@string/Title" /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center|fill_horizontal" 
      android:orientation="horizontal" 
      android:padding="2dp" > 

      <Button 
       android:id="@+id/btnLeft" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="2dp" 
       android:layout_weight="1" 
       android:text="@string/LeftButton" /> 

      <Button 
       android:id="@+id/btnRight" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="2dp" 
       android:layout_weight="1" 
       android:text="@string/RightButton" /> 

     </LinearLayout> 
    </LinearLayout> 

那麼,所有這些代碼完全適用於我想要什麼,但我得到了我的兩個按鈕的警告。它說,「嵌套的權重是壞的表現」。我知道這是什麼,但我不知道如何改變我的佈局,以擺脫警告(並提高性能,我想),並保持兩個按鈕具有一半的屏幕。

任何想法?謝謝!

+0

我的問題是,我得到的警告消息 – ali 2012-04-22 14:52:08

+0

該警告信息說明了什麼?我們... – 2012-04-22 14:52:49

+0

「嵌套的重量對性能不利」 – ali 2012-04-22 14:54:35

回答

6

試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txtTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:paddingTop="5dp" 
     android:text="@string/Title" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="2dp" 
     android:layout_marginLeft="2dp" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btnLeft" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/LeftButton" /> 

     <Button 
      android:id="@+id/btnRight" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/RightButton" /> 

    </LinearLayout> 
</LinearLayout> 
+0

我試過了,但是警告仍然存在 – ali 2012-04-22 14:52:57

+0

使用lint工具來尋找其中的埃羅becasue放大器解決方案是正確的形式使用layout_weight來避免視圖(按鈕)的2個builts – Aracem 2012-04-22 15:16:50

1
<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center|fill_horizontal" 
      android:orientation="horizontal" 
      android:padding="2dp" > 

替換上面下面「:::

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:padding="2dp" android:weighSum = "2"> 
+0

不起作用。也許我應該嘗試相對佈局,即使我討厭重新編輯整個文件 – ali 2012-04-22 14:55:39

1
<LinearLayout 
    android:id="@+id/buttonHolder" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 

    <Button 
     android:id="@+id/cmdSignup" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/Signup" /> 

    <Button 
     android:id="@+id/cmdLogin" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/Login" /> 
</LinearLayout>