【问题标题】:Put text at the right end of the row将文本放在行的右端
【发布时间】:2016-11-05 22:20:43
【问题描述】:

我有一个 CardView,里面有一个表格布局。在我的表格布局中,我希望文本视图出现在行的右端。

我的 xml

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        card_view:cardUseCompatPadding="true"
        app:cardElevation="@dimen/cardView_elevation"
        app:cardCornerRadius="@dimen/cardView_radius"
        card_view:cardBackgroundColor="@color/cardView_background">
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp" android:layout_marginBottom="10dp">
            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/row_background"
                android:padding="2dp">
                <TextView
                    android:text="Under Weight"
                    style="@style/RowLabel" android:textColor="@android:color/holo_orange_light"/>
                <TextView
                    android:text="&lt;18.5"
                    style="@style/RowVal" android:textColor="@android:color/holo_orange_light"/>
            </TableRow>

... 其他一些与上面相同的行

我的风格:

<style name="RowLabel" parent="@android:style/TextAppearance">
    <item name="android:textSize">14.0sp</item>
    <item name="android:paddingLeft">10.0dip</item>
    <item name="android:paddingRight">20.0dip</item>
</style>
<style name="RowVal" parent="@android:style/TextAppearance">
    <item name="android:textSize">14.0sp</item>
    <item name="android:paddingLeft">20.0dip</item>
    <item name="android:paddingRight">10.0dip</item>
</style>

图片:

【问题讨论】:

  • 在xml中使用重力属性
  • gravity="end" 或gravity="right" 会解决你的问题

标签: android user-interface textview android-tablelayout cardview


【解决方案1】:

第 1 步:创建 .Xml 文件

<LinearLayout style="@style/summaryField">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1.60">

                    <TextView
                        android:id="@+id/tv_lname_summary"                           
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="10dp"
                        android:gravity="center_vertical" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2.40">

                    <TextView
                        android:id="@+id/tv_getlname_summary"                            
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="3"
                        android:background="@color/userDataColor"
                        android:gravity="center_vertical" />

                </LinearLayout>

            </LinearLayout>

第 2 步:在 styles.xml 中创建样式

 //Advert Summary Screen
    <style name="summaryField">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:weightSum">4</item>
    </style>

【讨论】:

  • 对于每一行,您都可以使用这种格式的线性布局
【解决方案2】:

试试这个:

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="&lt;18.5"
                android:layout_weight="1"
                android:gravity="right"
                android:textColor="@android:color/holo_orange_light" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多