2016-02-29 26 views
0

我在Android studio的列表視圖中創建了一個表格視圖(參見下面的代碼),並且出於各種原因,我使用了文本視圖和複選框作爲單獨的元素,而不是隻是有複選框並附上文本。問題是文本位於屏幕/表格的左側,但複選框基本居中,因爲我希望它位於最右側。Android studio移動複選框到表單元格右邊

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/title" 
    android:layout_above="@+id/Button1" 
    android:id="@+id/scrollView" 
    android:background="@drawable/list"> 

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/Maths" 
     android:id="@+id/Maths" 
     android:layout_column="1" 
     android:textColor="#ffffff" /> 
     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/MathsCheck" 
      android:layout_column="2" 
      android:buttonTint="#00ffff" 
      android:onClick="MathsClicked"/> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="@string/History" 
      android:id="@+id/History" 
      android:layout_column="1" 
      android:textColor="#ffffff"/> 
     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/HistoryCheck" 
      android:layout_column="2" 
      android:onClick="HistoryClicked" 
      android:buttonTint="#00ffff" /> 
    </TableRow> 
</TableLayout> 
</ScrollView> 

實際上有很多這些文本視圖和複選框,但你明白了。我曾嘗試使用填充和邊距,但這並不能真正起作用,因爲它在某些設備顯示器上可以正常工作,但我必須對其進行硬編碼,這並不理想。我也嘗試過由於某種原因似乎沒有任何作用的重力功能。

任何幫助表示讚賞

回答

0

有爲什麼要使用TableLayout一個特別的原因? 如果不是,則可以使用垂直LinearLayout中的RelativeLayout輕鬆實現所需的輸出(TextView左對齊,右對齊的複選框)。然後

你的XML是這樣的(僅包括相關的部分,你的代碼的所有其他屬性保持不變):

<ScrollView> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_alignParentLeft="true" /> 
     <CheckBox 
      android:layout_alignParentRight="true" /> 
    </RelativeLayout> 

</LinearLayout> 
</ScrollView> 

編輯:如果要放置多個複選框 旁邊對方,你可以通過自己的位置相互借鑑,像這樣

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_alignParentLeft="true" /> 

    <CheckBox 
     android:id="@+id/checkbox1" 
     android:layout_alignParentRight="true" /> 

    <CheckBox 
     android:id="@+id/checkbox2" 
     android:layout_toLeftOf="@id/checkbox1" /> 

    <CheckBox 
     android:id="@+id/checkbox3" 
     android:layout_toLeftOf="@id/checkbox2" /> 
</RelativeLayout> 

請注意,您必須首先定義最右邊的複選框,最左邊的最後這樣做。 鑑於複選框的寬度是相同的(它們在大多數情況下),它們將完美對齊。

+0

我的意思是肯定和否,對我來說實現我想用垂直線性佈局實現的設計是可行的,事情是我可能希望將來在一個textview旁邊添加幾個複選框並且具有一些文本視圖比如有3個複選框,有些是1,但是第三個複選框總是需要對齊,所以我選擇了表格佈局。 –

+0

您提到的場景仍然可以使用RelativeLayouts實現。我會相應地編輯我的答案。 – Phoca

+0

非常感謝,我會研究它,然後報告回來 –