繁体   English   中英

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

[英]Align content of cell in TableLayout to the right

我是android开发的新手。 我有一个TableLayout活动。 该活动的构建如下图所示。

活动

我希望两个较小的按钮在右侧,以便两个EditText都可以使用剩余的全部宽度。 我还尝试像建议的一些帖子一样设置“ android:gravity”,但没有任何效果。 我该如何完成?

此外,这两行的信息,文本和按钮都有2个tableRows。 活动的其余部分不在任何tableRow中,以使其具有全角。 那是通用还是不好的编程? 如果是这样,更好的方法是什么?

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


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


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

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

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <EditText
            android:layout_width="350px"
            android:layout_height="wrap_content"
            android:id="@+id/txtOutputRGB"
            android:textCursorDrawable="@null"
            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/cmdCopyRGBToClipboard"
        android:layout_column="2"
        android:layout_gravity="right"/>
</TableRow>


<TableRow
    android:layout_width="wrap_content"
    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_column="2"/>
</TableRow>


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

尝试将tablerow设置为fill_parent,然后可以使用android:layout_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>

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

例如,您的EditText可以是:

<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分配尽可能多的空间,而其他视图获得的空间最小。渲染所需的空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM