简体   繁体   English

如何使用 LinearLayout 将 TextView 向右对齐

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

I have two textView objects that I am using to display the result of the calculations in my app.我有两个 textView 对象,用于在我的应用程序中显示计算结果。 They are showing up to the right of the TextView so I wanted to know if there is a way through the xml file to position the TextView to the right.它们显示在 TextView 的右侧,所以我想知道是否有办法通过 xml 文件将 TextView 定位到右侧。 This is my xml code:这是我的 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>

它应该看起来如何

Make sure your view matches the parent, then add the gravity of its content to the right:确保您的视图与父视图匹配,然后将其内容的重力添加到右侧:

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

Hope it helped ;)希望它有所帮助;)

Just add the line只需添加行

android:gravity="right"

to the textview you want aligned left.到要左对齐的文本视图。

You can do something like this你可以做这样的事情

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

Please check that Linear Layout has an attribute named GRAVITY and inner control(ImageView) has an attribute named LAYOUT_GRAVITY both are different请检查 Linear Layout 有一个名为 GRAVITY 的属性,而内部控件(ImageView)有一个名为 LAYOUT_GRAVITY 的属性,两者都不同

Sure.当然。 Just change the order of the elements inside the LinearLayout.只需更改 LinearLayout 中元素的顺序即可。 Make sure the EditText field is first and then the TextView field.确保首先是 EditText 字段,然后是 TextView 字段。 Easy.简单。

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

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