简体   繁体   English

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

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

I'm relatively new to android development. 我是android开发的新手。 I have an activity with a TableLayout. 我有一个TableLayout活动。 The activity is built like in the image below. 该活动的构建如下图所示。

活动

I want the two smaller buttons to be on the right, so that both EditTexts can use the full remaining width. 我希望两个较小的按钮在右侧,以便两个EditText都可以使用剩余的全部宽度。 I also tried to set "android:gravity" like some posts suggested, but without any effects. 我还尝试像建议的一些帖子一样设置“ android:gravity”,但没有任何效果。 How do I get that done? 我该如何完成?

Additionally, I have 2 tableRows for the both lines with the info, text & button. 此外,这两行的信息,文本和按钮都有2个tableRows。 The rest of the activity is out of any tableRow to get them full-width. 活动的其余部分不在任何tableRow中,以使其具有全角。 Is that common-use or bad programming? 那是通用还是不好的编程? If so, how is the better way? 如果是这样,更好的方法是什么?

<?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>

Try setting the tablerow to fill_parent and then you can use android:layout_gravity="right" 尝试将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>

A TableRow layout is exactly like a LinearLayout (in fact, it's a subclass of LinearLayout), so you can use the layout_weight property from LinearLayout to allow a view in a row to expand to fill space in relation to the other views in its row. TableRow布局与LinearLayout完全一样(实际上,它是LinearLayout的子类),因此您可以使用LinearLayout中的layout_weight属性来允许一行中的视图相对于该行中的其他视图展开以填充空间。

For example, your EditText could be: 例如,您的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"/>

Note that the width is 0 and the weight is 1. When the other views in the row have a weight of 0 (the default) This tells TableRow to allocate as much space as it can to the EditText while the other views get the minimal amount of space required to render. 请注意,宽度为0,权重为1。当行中的其他视图的权重为0(默认值)时,这告诉TableRow为EditText分配尽可能多的空间,而其他视图获得的空间最小。渲染所需的空间。

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

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