如何在Android布局中的同一列上对齐
我有2个文本视图,我希望它们在同一列中水平对齐。我尝试 Android:layout_torightof =“@ID/textName”
,但似乎它变为底部,而是在 textName
moviedate
应在 moviename
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:cardBackgroundColor="#FFFFFF"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageview"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:src="@drawable/candy" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Movie Name"
android:textColor="#000"
android:layout_marginRight="100dp"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textdate"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Movie Date"
android:textSize="15sp"
android:layout_toRightOf="@id/textName"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
layout_torightof
是relativelayout
param,它是针对linearlayout
的儿童设置的no-op设置,因此您可以安全地删除tihs line,它无能为力。 。我看到代码中有很多问题和潜在的改进,因此也许您还应该设置
android:layout_height =“ wrap_content”
for此linearlayout
和android:dravelity:dravelity: =“ center_vertical”
for ofter One(cardView
的第一个孩子),还删除android:layout_weight =“ 2”
,它的不必要,因为此布局具有match_parent
宽度layout_toRightOf
is anRelativeLayout
param, it is no-op set for child ofLinearLayout
, so you can safely remove tihs line, it does nothing...LinearLayout
which is a parent of bothTextView
s should have setandroid:orientation="horizontal"
, thats for shure... but I see a lot of problems and potential improvements in your code, so maybe you should also setandroid:layout_height="wrap_content"
for thisLinearLayout
andandroid:gravity="center_vertical"
for outer one (first child ofCardView
), also removeandroid:layout_weight="2"
, its unnecessary as this layout havematch_parent
width使用Linearlayout并将定向设置为水平。请参阅此
Use LinearLayout and set orientation to horizontal instead . See this ????????
The layout_weight attribute with value of 50 gives equal portions to each children i.e 50% each
我认为Linearlayout是最简单的解决方案,但是您也可以尝试使用ConstraintLayout。
LinearLayout is the easiest solution in my opinion, but you could also try ConstraintLayout.
而不是使用多个 Linearlayout ,您可以使用单个 ConstraintLayout 并实现相同的目标。这是修改的代码:
Instead of using multiple LinearLayout you can use single ConstraintLayout and achieve same goal. Here is the modified code: