【问题标题】:Android studio move check box to right of table cellAndroid Studio 将复选框移动到表格单元格的右侧
【发布时间】:2016-03-01 08:21:51
【问题描述】:

我在 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>

实际上有更多这样的文本视图和复选框,但你明白了。我尝试过使用填充和边距,但这并没有真正起作用,因为它在某些设备显示器上可以正常工作,但我必须对其进行硬编码,这并不理想。我还尝试了重力功能,由于某种原因似乎没有任何作用。

任何帮助表示赞赏

【问题讨论】:

    标签: android checkbox textview


    【解决方案1】:

    您使用 TableLayout 有什么特别的原因吗? 如果没有,可以使用垂直 LinearLayout 中的RelativeLayout 轻松实现所需的输出(TextViews 左对齐,Checkboxes 右对齐)。

    然后您的 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>
    

    请注意,您必须首先定义最右边的复选框,最后定义最左边的复选框。 鉴于复选框的宽度是相同的(大多数情况下都是如此),它们将完全对齐。

    【讨论】:

    • 我的意思是是和不是,对我来说用垂直线性布局实现我想要实现的设计是可行的,问题是我将来可能想在一个文本视图旁边添加几个复选框并且有一些文本视图,例如 3 个复选框,一些有 1,但第三个总是需要对齐,因此我选择了表格布局。
    • 你提到的场景仍然可以使用RelativeLayouts来实现。我会相应地编辑我的答案。
    • 非常感谢,我会调查一下然后再报告
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 2015-08-15
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多