在Android布局的左右边缘对齐textviews
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2099249/
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
Aligning textviews on the left and right edges in Android layout
提问by Richard
I am getting started with Android. I am having trouble getting a simple layout going.
我开始使用Android。我无法进行简单的布局。
I would like to use a LinearLayout
to position two TextViews
in a single row. One TextView
on the left hand side, the other on the right hand side (analogous to float:left, float:right in CSS).
我想用 aLinearLayout
将两个TextViews
放在一行中。一个TextView
在左侧,另一个在右侧(类似于 CSS 中的 float:left、float:right)。
Is that possible, or do I need to use a different ViewGroup
or further layout nesting to accomplish it?
这是可能的,还是我需要使用不同的ViewGroup
或进一步的布局嵌套来完成它?
Here's what I have so far:
这是我到目前为止所拥有的:
<?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>
回答by Dave Webb
Use a RelativeLayout
with layout_alignParentLeft
and layout_alignParentRight
:
使用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>
Also, you should probably be using dip
(or dp
) rather than sp
in your layout. sp
reflect text settings as well as screen density so they're usually only for sizing text items.
此外,您可能应该使用dip
(或dp
) 而不是sp
在您的布局中。 sp
反映文本设置以及屏幕密度,因此它们通常仅用于调整文本项的大小。
回答by JeremyFromEarth
You can use the gravity property to "float" views.
您可以使用重力属性来“浮动”视图。
<?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>
回答by Carson Holzheimer
It can be done with LinearLayout
(less overhead and more control than the Relative Layout option). Give the second view the remaining space so gravity
can work. Tested back to API 16.
它可以通过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>
If you want to limit the size of the first text view, do this:
如果要限制第一个文本视图的大小,请执行以下操作:
Adjust weights as required. Relative layout won't allow you to set a percentage weight like this, only a fixed dp of one of the views
根据需要调整重量。相对布局不允许您设置这样的百分比权重,只有一个视图的固定 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>
回答by pumpkinpie65
Even with Rollin_s's tip, Dave Webb's answer didn't work for me. The text in the right TextView
was still overlapping the text in the left TextView
.
即使有 Rollin_s 的提示,Dave Webb 的回答对我也不起作用。右边TextView
的文字仍然与左边的文字重叠TextView
。
I eventually got the behavior I wanted with something like this:
我最终得到了我想要的行为:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:id="@+id/mytextview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/mytextview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/mytextview1"
android:gravity="right"/>
</RelativeLayout>
Note that mytextview2 has "android:layout_width"
set as "match_parent"
.
请注意, mytextview2 已"android:layout_width"
设置为"match_parent"
.
Hope this helps someone!
希望这可以帮助某人!
回答by Taj Mb
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello world" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gud bye" />
回答by funct7
In case you want the left and right elements to wrap content but have the middle space
如果您希望左右元素包装内容但有中间空间
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
回答by Sayed Muhammad Idrees
This Code will Divide the control into two equal sides.
此代码将控件分成两个相等的边。
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout">
<TextView
android:text = "Left Side"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
android:id = "@+id/txtLeft"/>
<TextView android:text = "Right Side"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
android:id = "@+id/txtRight"/>
</LinearLayout>
回答by NelsonRoberts
There are many other ways to accomplish this, I would do something like this.
有很多其他方法可以实现这一点,我会做这样的事情。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:text="YOUR TEXT ON THE RIGHT"
android:textSize="16sp"
android:textStyle="italic" />
<TextView
android:id="@+id/textLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp"
android:text="YOUR TEXT ON THE LEFT"
android:textSize="16sp" />
</RelativeLayout>
回答by GinCanhViet
Dave Webb's answer did work for me. Thanks! Here my code, hope this helps someone!
Dave Webb 的回答对我有用。谢谢!这是我的代码,希望这对某人有所帮助!
<RelativeLayout
android:background="#FFFFFF"
android:layout_width="match_parent"
android:minHeight="30dp"
android:layout_height="wrap_content">
<TextView
android:height="25dp"
android:layout_width="wrap_content"
android:layout_marginLeft="20dp"
android:text="ABA Type"
android:padding="3dip"
android:layout_gravity="center_vertical"
android:gravity="left|center_vertical"
android:layout_height="match_parent" />
<TextView
android:background="@color/blue"
android:minWidth="30px"
android:minHeight="30px"
android:layout_column="1"
android:id="@+id/txtABAType"
android:singleLine="false"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_width="wrap_content" />
</RelativeLayout>
Image: Image
图像: 图像