2017-05-17 53 views
0

我需要使用GridLayout來顯示兩個按鈕。一個和第二個與其他按鈕的寬度的兩倍。我想NEXT按鈕的寬度是BACK按鈕的兩倍,我需要一個GridLayout無法顯示兩個按鈕,其中一個按鈕的寬度是GridLayout中其他寬度的兩倍

我的源代碼是如下:

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    style="@style/Calculator.Grid" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:columnCount="3" 
    app:rowCount="1"> 

    <Button 
     android:id="@+id/button_back" 
     android:layout_column="0" 
     android:layout_columnSpan="1" 
     android:layout_columnWeight="1" 
     android:layout_gravity="left|top" 
     android:background="@drawable/bg_back_button" 
     android:text="@string/back" 
     android:textColor="?attr/colorTextSecondary" 
     android:textSize="16sp" /> 

    <Button 
     android:id="@+id/button_next" 
     android:layout_column="1" 
     android:layout_columnSpan="2" 
     android:layout_columnWeight="2" 
     android:layout_gravity="fill" 
     android:background="@drawable/bg_next_button" 
     android:text="@string/next" 
     android:textColor="?attr/colorTextPrimary" 
     android:textSize="16sp" /> 
</GridLayout> 

希望的顯示(NEXT按鈕應返回按鈕的雙寬度):

Desired Display

但結果是(問題):

Result display

+0

你需要* *'GridView控件'?或者你可以在'LinearLayout'中包裝'Button'? – Abbas

+0

@Sandeep Shakya請檢查我的答案是完全正常的。 –

+0

@Abbas是的,我需要一個GridLayout在我的情況。 –

回答

0

如果沒有與線性佈局任何問題,因爲內膽然後用這個

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
style="@style/Calculator.Grid" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:columnCount="3" 
app:rowCount="1"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button_back" 
     android:layout_column="0" 
     android:layout_columnSpan="1" 
     android:layout_columnWeight="1" 
     android:layout_gravity="left|top" 
     android:background="@drawable/bg_back_button" 
     android:text="@string/back" 
     android:textColor="?attr/colorTextSecondary" 
     android:textSize="16sp" 
     android:layout_weight="2"/> 


    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button_next" 
     android:layout_column="1" 
     android:layout_columnSpan="2" 
     android:layout_columnWeight="2" 
     android:layout_gravity="fill" 
     android:background="@drawable/bg_next_button" 
     android:text="@string/next" 
     android:textColor="?attr/colorTextPrimary" 
     android:textSize="16sp" 
     android:layout_weight="1"/> 

</LinearLayout> 




</GridLayout> 
+1

哇!由問題解決。非常感謝@Abhishek。 –

0

這是工作,我的兄弟試試這個

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:columnCount="3" 
android:rowCount="3"> 

<Button 
    android:id="@+id/textViewNW" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_columnSpan="1" 
    android:layout_columnWeight="1" 
    android:layout_gravity="center|fill" 
    android:layout_rowSpan="1" 
    android:layout_rowWeight="1" 
    android:background="#fe4141" 
    android:text="@string/app_name" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/textViewNE" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_columnSpan="2" 
    android:layout_columnWeight="2" 
    android:layout_gravity="center|fill" 
    android:layout_rowSpan="2" 
    android:layout_rowWeight="2" 
    android:background="#51f328" 
    android:text="@string/app_name" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</GridLayout> 
+0

對不起兄弟,但是這個源代碼與我想要的有點不同。第一個按鈕比第二個按鈕的一半大一點。 Mr @ Abhishek先前提出的解決方案。 –

0
<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    style="@style/Calculator.Grid" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:columnCount="3" 
    app:rowCount="1"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="3"> 

     <Button 
      android:id="@+id/button_back" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="0" 
      android:layout_columnSpan="1" 
      android:layout_columnWeight="1" 
      android:layout_gravity="left|top" 
      android:layout_weight="2" 
      android:background="@drawable/bg_back_button" 
      android:text="@string/back" 
      android:textColor="?attr/colorTextSecondary" 
      android:textSize="16sp" /> 

     <Button 
      android:id="@+id/button_next" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_columnSpan="2" 
      android:layout_columnWeight="2" 
      android:layout_gravity="fill" 
      android:layout_weight="1" 
      android:background="@drawable/bg_next_button" 
      android:text="@string/next" 
      android:textColor="?attr/colorTextPrimary" 
      android:textSize="16sp" /> 
    </LinearLayout> 

</GridLayout>