Android 如何将 textview 放置在布局的右侧和左侧?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3581379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to place textview on right and left of layout?
提问by Sheehan Alam
I want to place a textview on the right of a layout and on the left of a layout, but they keep stacking ontop of each other:
我想在布局的右侧和布局的左侧放置一个 textview,但它们一直堆叠在一起:
<LinearLayout
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent" android:orientation="horizontal">
<TextView
android:id="@+id/lefttext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="Symbol"/>
<TextView
android:id="@+id/righttext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="Price"/>
</LinearLayout>
回答by Kevin Coppock
Try this:
尝试这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/left_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Symbol"
/>
<TextView
android:id="@+id/right_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Price"
/>
</LinearLayout>
Why are you specifying 0dip for the height on these?
为什么要为这些高度指定 0dip?
For symbols on sides:
对于侧面的符号:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/left_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:gravity="left"
android:text="Symbol"
/>
<TextView
android:id="@+id/right_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:gravity="right"
android:text="Price"
/>
</LinearLayout>
And done with a RelativeLayout:
并使用 RelativeLayout 完成:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:text="Code"
/>
<TextView
android:id="@+id/right_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_toRightOf="@+id/left_text"
android:gravity="right"
android:text="Company Name"
/>
</RelativeLayout>
回答by RobGThai
Use android:layout_alignParentLeft="true"
and android:layout_alignParentRight="true"
on view inside RelaytiveLayout
like this
像这样使用android:layout_alignParentLeft="true"
和android:layout_alignParentRight="true"
查看内部RelaytiveLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left|center_vertical"
android:text="Symbol"
/>
<TextView
android:id="@+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right|center_vertical"
android:text="Price"
/>
</RelativeLayout>
Alternatively you can use TableLayout
with stretchColumn
set to stretch the second column to the size of parent.
或者,您可以使用TableLayout
with stretchColumn
set 将第二列拉伸到父列的大小。
回答by AppiDevo
With LinearLayout, just put an empty TextView with the weight between them:
使用 LinearLayout,只需在它们之间放置一个空的 TextView 和它们之间的权重:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/lefttext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Symbol"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/righttext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Price"/>
</LinearLayout>