我开始使用Android了。我在进行一个简单的布局时遇到了麻烦。
我想使用LinearLayout
将两个TextViews
放置在一行中。一个TextView
在左边,另一个在右边(类似于CSS中的float: left,float:right )。
这是可能的吗,或者我需要使用不同的ViewGroup
或进一步的布局嵌套来完成它?
这是我到目前为止所知道的:
<?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" android:padding="10sp">
<TextView android:id="@+id/mytextview1" android:layout_height="wrap_content" android:text="somestringontheleftSomestring" android:layout_width="wrap_content"/>
<TextView android:id="@+id/mytextview2" android:layout_height="wrap_content" android:ellipsize="end"
android:text="somestringontheright" android:layout_width="wrap_content"/>
</LinearLayout>
发布于 2010-01-20 14:38:16
将RelativeLayout
与layout_alignParentLeft
和layout_alignParentRight
一起使用
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/mytextview1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/mytextview2"/>
</RelativeLayout>
还有,you should probably be using dip
(or dp
) rather than sp
in your layout。sp
反映文本设置以及屏幕密度,因此它们通常仅用于调整文本项的大小。
发布于 2010-01-20 15:11:00
你可以使用重力属性来“浮动”视图。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:text="Left Aligned"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:text="Right Aligned"
/>
</LinearLayout>
</LinearLayout>
发布于 2018-03-07 21:22:57
这可以使用LinearLayout
完成(比相对布局选项更少的开销和更多的控制)。让第二个视图留出剩余空间,这样gravity
就可以工作了。已测试回API 16。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aligned left" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="Aligned right" />
</LinearLayout>
如果要限制第一个文本视图的大小,请执行以下操作:
根据需要调整权重。相对布局不允许您像这样设置百分比权重,只能设置其中一个视图的固定dp
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Aligned left but too long and would squash the other view" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="end"
android:text="Aligned right" />
</LinearLayout>
https://stackoverflow.com/questions/2099249
复制相似问题