![](/img/trans.png)
[英]Android TextView Right Align in LinearLayout Programmatically
[英]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.