如何正确对齐此布局?
我有两个文本视图。我希望其中一个左对齐,另一个右对齐。几乎使它看起来像一个 2 列表。
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/profileLayout" android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/experience" android:orientation="horizontal">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/experienceLabel" android:textStyle="bold" android:layout_weight="0" android:text="Experience"></TextView>
<LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"></LinearLayout>
<TextView android:text="TextView" android:layout_height="wrap_content" android:id="@+id/experienceTextView" android:layout_gravity="right" android:layout_weight="0" android:layout_width="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
I have two textviews. I want one of them to be left aligned and the other to be right aligned. Pretty much so that it looks like a 2 column table.
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/profileLayout" android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/experience" android:orientation="horizontal">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/experienceLabel" android:textStyle="bold" android:layout_weight="0" android:text="Experience"></TextView>
<LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"></LinearLayout>
<TextView android:text="TextView" android:layout_height="wrap_content" android:id="@+id/experienceTextView" android:layout_gravity="right" android:layout_weight="0" android:layout_width="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将第一个的权重设置为 1,这样它将占用所有额外的空间。将第二个打开的内容设置为包裹内容,因此它只占用所需的空间。
但是为什么它们周围有一个额外的 LinearLayout 呢?如果您有嵌套的 LinearLayout,您可能应该使用relativelayout。
Set the weight of the first one to 1 so it will eat up all of the extra space. Set the 2nd oen to wrap content, so it only takes up as much space as it needs.
But why do you have an extra LinearLayout around them? If you have nested LinearLayouts, you should probably be using a RelativeLayout instead.
使用相对布局。关于相对布局 链接 示例链接
使用RelativeLayout的好处:它减少了布局的数量。这也会优化你的用户界面。
Use Relative Layout. About Relative Layout Link Example link
Benefit of using RelativeLayout: it reduces your number of layout. Which will optimize your ui too.
我不完全确定您想要的确切布局,但您可以尝试一下:
您也使用
layout_gravity
而不是gravity
。layout_gravity
告诉父级您想要如何布局,gravity
告诉内容如何布局。请注意,我如何将父布局中的
weightSum
设置为 2,然后将两个子布局的权重分别设置为 1。我还将父级设置为fill_parent
你在那里也有额外的布局 - 如果你想要间距,请在第二个文本视图上设置边距或使用视图(比 LinearLayout 更简单的对象)
I'm not entirely sure of the exactly layout you want, but you can try this:
You were using
layout_gravity
rather thangravity
also.layout_gravity
tells the parent how you want to be laid out,gravity
tells content how to be laid out.Notice how I have a
weightSum
in the parent layout to 2 and then set the two children to a weight of 1 each. I also set the parent tofill_parent
You had and extra layout in there too - if you want spacing, set a margin on the second textview or use a view (a simpler object than a LinearLayout)