有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将TableLayout中单元格的内容向右对齐

我对安卓系统的开发还比较陌生。我有一个关于桌子布局的活动。该活动如下图所示

Activity

我希望两个较小的按钮位于右侧,这样两个EditText都可以使用剩余的全部宽度。我还试着像一些帖子建议的那样设置“安卓:gravity”,但没有任何效果。我该怎么做

此外,我有两个表格行,用于两行的信息、文本和;按钮 活动的其余部分不在任何表格行中,以使其达到最大宽度。 这是常用还是糟糕的编程?如果是这样,更好的方法是什么

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent" 安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
安卓:orientation="vertical"
安卓:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <TextView
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:textAppearance="?安卓:attr/textAppearanceMedium"
        安卓:text="@string/strInputInfo"
        安卓:id="@+id/lblHexToRGB"
        安卓:layout_column="1" />


    <EditText
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:id="@+id/txtHexInput"
        安卓:layout_column="1"
        安卓:textCursorDrawable="@null"
        安卓:hint="@string/strHexInputHint" />

    <Button
        安卓:layout_width="fill_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/strConvertButtonText"
        安卓:id="@+id/cmdConvertCode"
        安卓:layout_column="1"/>

<TableRow
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <TextView
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceMedium"
            安卓:text="@string/strRGBInfo"
            安卓:id="@+id/lblRGBInfo"
            安卓:layout_column="0" />

    <EditText
            安卓:layout_width="350px"
            安卓:layout_height="wrap_content"
            安卓:id="@+id/txtOutputRGB"
            安卓:textCursorDrawable="@null"
            安卓:layout_column="1"
        安卓:textAlignment="center"/>

    <Button
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="@string/strButtonCopyText"
        安卓:id="@+id/cmdCopyRGBToClipboard"
        安卓:layout_column="2"
        安卓:layout_gravity="right"/>
</TableRow>


<TableRow
    安卓:layout_width="wrap_content"
    安卓:layout_height="match_parent">

    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:textAppearance="?安卓:attr/textAppearanceMedium"
        安卓:text="@string/strHexInfo"
        安卓:id="@+id/lblHexInfo"
        安卓:layout_column="0"/>

    <EditText
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:id="@+id/txtOutputHex"
        安卓:layout_column="1"
        安卓:textAlignment="center"/>

    <Button
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="@string/strButtonCopyText"
        安卓:id="@+id/cmdCopyHexToClipboard"
        安卓:layout_column="2"/>
</TableRow>


<ImageView
            安卓:layout_width="fill_parent"
            安卓:layout_height="wrap_content"
            安卓:id="@+id/imageView"
            安卓:layout_column="1"
            安卓:maxHeight="100dp"
            安卓:minHeight="100dp" />
</TableLayout>

共 (2) 个答案

  1. # 1 楼答案

    尝试将tablerow设置为填充父对象,然后可以使用android:layout\u gravity=“right”

    <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/textAppearanceMedium"
            android:text="@string/strHexInfo"
            android:id="@+id/lblHexInfo"
            android:layout_column="0"/>
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtOutputHex"
            android:layout_column="1"
            android:textAlignment="center"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/strButtonCopyText"
            android:id="@+id/cmdCopyHexToClipboard"
            android:layout_gravity="right"
            android:layout_column="2"/>
    </TableRow>
    
  2. # 2 楼答案

    TableRow布局与LinearLayout完全相同(事实上,它是LinearLayout的子类),因此可以使用LinearLayout中的layout_weight属性来允许一行中的视图展开,以相对于其行中的其他视图填充空间

    例如,您的编辑文本可以是:

    <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/txtOutputRGB"
            android:textCursorDrawable="@null"
            android:layout_column="1"
            android:textAlignment="center"/>
    

    请注意,宽度为0,权重为1。当行中的其他视图的权重为0(默认值)时,这会告诉TableRow为EditText分配尽可能多的空间,而其他视图获得渲染所需的最小空间