0

我有一個嵌套的權重,這對於pefrormance很糟糕的佈局。 如何製作計算器鍵盤(例如沒有重量的計算器鍵盤),但是在不給予固定「sp」或「dp」大小的情況下節省按鈕大小,無論屏幕上的分辨率如何,都必須縮放鍵盤實現鍵盤佈局,沒有嵌套的重量和固定的尺寸在xml

,我發現這裏的解決方案CommonsWare answer這裏Che Jame answer

但爲什麼他們說:「使用Android:layout_weight =」鍵上的兩個」 1" Eclipse的說我必須得到權重的蘆葦... 是還有其他解決方案嗎?或者在android平臺上不可能? 這裏是我有它使用嵌套的權重,而代碼如下

我要救這個觀點,但bithout嵌套的權重和按鈕必須是相同的大小,更大然後「包裝內容」參數

感謝您的任何建議。

<LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="0sp" 
         android:layout_weight="1" 
         android:orientation="vertical" > 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="0sp" 
          android:layout_weight="1" 
          android:orientation="horizontal" > 

          <Button 
           android:id="@+id/cashcount_form_button1" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_1" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 

          <Button 
           android:id="@+id/cashcount_form_button2" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_2" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 

          <Button 
           android:id="@+id/cashcount_form_button3" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_3" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 
         </LinearLayout> 

         <LinearLayout 
          android:layout_width="fill_parent" 
          android:layout_height="0sp" 
          android:layout_weight="1" 
          android:orientation="horizontal" > 

          <Button 
           android:id="@+id/cashcount_form_button4" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_4" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 

          <Button 
           android:id="@+id/cashcount_form_button5" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_5" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 

          <Button 
           android:id="@+id/cashcount_form_button6" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_6" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 
         </LinearLayout> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="0sp" 
          android:layout_weight="1" 
          android:orientation="horizontal" > 

          <Button 
           android:id="@+id/cashcount_form_button7" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_7" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 

          <Button 
           android:id="@+id/cashcount_form_button8" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_8" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 

          <Button 
           android:id="@+id/cashcount_form_button9" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_9" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 
         </LinearLayout> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="0sp" 
          android:layout_weight="1" 
          android:orientation="horizontal" > 

          <Button 
           android:id="@+id/cashcount_form_comma_button" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_comma" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 

          <Button 
           android:id="@+id/cashcount_form_button0" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_weight="1" 
           android:text="@string/cashcount_key_0" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:textColor="@color/white" 
           android:textSize="35sp" 
           android:textStyle="bold" /> 
         <Button 
          android:id="@+id/cashcount_form_delete_button" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" 
          android:drawableStart="@drawable/ic_action_back_sign_image_view" 
          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:textColor="@color/white" 
          android:textSize="35sp" 
          android:textStyle="bold" /> 
        </LinearLayout> 

       </LinearLayout> 

回答

0

suggested由Android LinearLayouts應由RelativeLayout的或GridLayout的來代替。在大多數情況下,RelativeLayout就足夠了,但在這種情況下GridLayout可以派上用場。你可以寫下類似於下面的代碼來實現你想要的東西:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:columnCount="4" 
    android:orientation="horizontal" > 

    <Button 
     android:layout_column="3" 
     android:text="/" /> 

    <Button android:text="1" /> 

    <Button android:text="2" /> 

    <Button android:text="3" /> 

    <Button android:text="*" /> 

    <Button android:text="4" /> 

    <Button android:text="5" /> 

    <Button android:text="6" /> 

    <Button android:text="-" /> 

    <Button android:text="7" /> 

    <Button android:text="8" /> 

    <Button android:text="9" /> 

    <Button 
     android:layout_gravity="fill" 
     android:layout_rowSpan="3" 
     android:text="+" /> 

    <Button 
     android:layout_columnSpan="2" 
     android:layout_gravity="fill" 
     android:text="0" /> 

    <Button android:text="00" /> 

    <Button 
     android:layout_columnSpan="3" 
     android:layout_gravity="fill" 
     android:text="=" /> 

</GridLayout>