2017-06-14 80 views
0

我在我的TableLayout裏面有兩個button,但文本不在中心,我嘗試使用android:gravity="center"和android android:layout_gravity="center",但似乎不起作用。文本的中心重力屬性在android按鈕中不起作用

我該如何解決這個問題?在此先感謝

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="20dp" 
     android:id="@+id/bawah" 
     android:layout_margin="10dp" 
     android:layout_alignParentBottom="true" 

     > 
     <TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:weightSum="10"> 
       <Button 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:text="Approve" 
        android:textColor="@color/putih" 
        android:textAlignment="center" 
        android:layout_gravity="center" 
        android:gravity="center" 
        android:layout_weight="4.9" 
        android:id="@+id/approve" 
        android:background="@drawable/backhijau" 

        /> 
       <TextView android:layout_height="wrap_content" 
        android:layout_width="0dip" 
        android:layout_weight="0.2"/> 
     <Button 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="4.9" 
      android:textAlignment="center" 
      android:text="Tolak" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:textColor="@color/putih" 
      android:id="@+id/tolak" 
      android:background="@drawable/backmerah" 
      /> 


      </TableRow> 
     </TableLayout> 
    </RelativeLayout> 

enter image description here

+0

不工作意味着什麼?請更清楚。 –

+0

我認爲你定義了權重,這就是爲什麼它不起作用 –

+0

海@jackjay,編輯我的問題 –

回答

2

這對我複製和過去的這段代碼,讓我知道

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/bawah" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_margin="10dp" 
    android:layout_marginRight="20dp"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="10"> 

      <Button 
       android:id="@+id/approve" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="4.9" 
       android:background="@color/colorAccent" 
       android:gravity="center" 
       android:text="Approve" 
       android:textAlignment="center" 
       android:textColor="@color/colorPrimary" 

       /> 

      <TextView 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.2" /> 

      <Button 
       android:id="@+id/tolak" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="4.9" 
       android:background="@color/colorPrimary" 
       android:gravity="center" 
       android:text="Tolak" 
       android:textAlignment="center" 
       android:textColor="@android:color/white" /> 

     </TableRow> 


</RelativeLayout> 

輸出

enter image description here

+0

Hai @ArpitPatel這個工作對我來說非常感謝,但爲什麼不需要包含TableLayout? –

+0

,因爲它包含或不包含您的佈局不會影響我們的佈局 –

+0

如果您需要多個表格,那麼您可以使用TableLayout –

2

否無似乎罰款TableLayout,只需使用RelativeLayoutandroid:layout_below財產,你會得到你想要的結果。也不需要執行weightSum屬性。

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="20dp" 
     android:id="@+id/bawah" 
     android:layout_margin="10dp"> 
       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Approve" 
        android:textColor="@color/putih" 
        android:textAlignment="center" 
        android:id="@+id/approve" 
        android:background="@drawable/backhijau" 

        /> 
       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textAlignment="center" 
        android:text="Tolak" 
        android:layout_below="@+id/approve" 
        android:textColor="@color/putih" 
        android:id="@+id/tolak" 
        android:background="@drawable/backmerah" 
        /> 

    </RelativeLayout> 
+0

海,感謝您的幫助@jackjay,我很感激,但我想讓我的按鈕在一行。 –

+0

你說你想讓他們全屏。這是你如何將它們設置爲全屏幕。 –