繁体   English   中英

如何使用 LinearLayout 将 TextView 向右对齐

[英]How to align a TextView to the right with LinearLayout

我有两个 textView 对象,用于在我的应用程序中显示计算结果。 它们显示在 TextView 的右侧,所以我想知道是否有办法通过 xml 文件将 TextView 定位到右侧。 这是我的 xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.jtryon.rectanglecalc.MainActivity$PlaceholderFragment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/width_string"
            android:textSize="16sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/width_edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"      
            android:inputType="numberDecimal" > 

        <requestFocus />
        </EditText>       

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >  

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/height_string"
        android:textSize="16sp"
        android:textStyle="bold" /> 

    <EditText
        android:id="@+id/height_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="numberDecimal" />     

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold" 
            android:text="@string/area_string" />

        <TextView
            android:id="@+id/area_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/perim_string"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"
            android:text="@string/perim_string" />


        <TextView
            android:id="@+id/perim_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp" />

        </LinearLayout>    

</LinearLayout>

它应该看起来如何

确保您的视图与父视图匹配,然后将其内容的重力添加到右侧:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/width_string" />
</LinearLayout>

希望它有所帮助;)

只需添加行

android:gravity="right"

到要左对齐的文本视图。

你可以做这样的事情

  <LinearLayout
    android:gravity="end"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/ic_call_white"
        />

</LinearLayout>

请检查 Linear Layout 有一个名为 GRAVITY 的属性,而内部控件(ImageView)有一个名为 LAYOUT_GRAVITY 的属性,两者都不同

当然。 只需更改 LinearLayout 中元素的顺序即可。 确保首先是 EditText 字段,然后是 TextView 字段。 简单。

暂无
暂无

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

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