Android RelativeLayout隐藏先前的线性布局
在某些应用程序中,我有一个表示单行的XML,包含名称、等级和图片 名称+等级垂直设置在左边的线条中,我的目的是让图片位于线条的右侧 我试图使用RelativeLayout,以便将ImageView与屏幕右侧对齐,因为我知道这是正确的方法,但它隐藏了我的其他布局- 使用图像视图时,只有图像视图可见 尝试更改布局宽度\高度以匹配父级,但没有帮助 我误解了什么?我在这里的错误用法是什么 XML:Android RelativeLayout隐藏先前的线性布局,android,android-layout,android-linearlayout,android-relativelayout,Android,Android Layout,Android Linearlayout,Android Relativelayout,在某些应用程序中,我有一个表示单行的XML,包含名称、等级和图片 名称+等级垂直设置在左边的线条中,我的目的是让图片位于线条的右侧 我试图使用RelativeLayout,以便将ImageView与屏幕右侧对齐,因为我知道这是正确的方法,但它隐藏了我的其他布局- 使用图像视图时,只有图像视图可见 尝试更改布局宽度\高度以匹配父级,但没有帮助 我误解了什么?我在这里的错误用法是什么 XML: 是否尝试将权重总和设置为第一个/根线性布局?如果没有,则将android:weightSum=3添加到根L
是否尝试将权重总和设置为第一个/根线性布局?如果没有,则将android:weightSum=3添加到根LL。在此之后,将layout_weight=2添加到您的RelativeLayout中,尝试以下操作: 将您的孩子按如下方式排列:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="start|center"
android:orientation="vertical">
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
我建议您对所有XML使用RelativeLayout,或者您可以查看ConstraintLayout。使用RelativeLayout可以让一切变得更简单
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/iv_pic"
android:gravity="start|center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:ellipsize="end"
android:focusable="false"
android:singleLine="true"
android:padding="2dp"
android:text="tv_exampleaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
android:textAlignment="center"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:ellipsize="end"
android:focusable="false"
android:inputType="number"
android:singleLine="true"
android:padding="2dp"
android:text="tv_example"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold|italic" />
</LinearLayout>
<ImageView
android:id="@+id/iv_pic"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:background="#ff0000" />
</RelativeLayout>
希望这是你想要的
通过将父LinearLayout替换为FrameLayout,可以轻松实现这一点
但是为了展示。。。在文本末尾,您必须在现有布局中使用权重或静态宽度为TextView指定固定宽度。现在尝试,TextView仅显示1个字符,尽管行中有大量可用空间显示整个文本,当ImageView向右对齐时,您可以更改线性布局和相对布局的布局权重值,以获得完美的结果,只需进行实验即可。我认为如果您将LinearLayout的layout_weight=更改为3,relativeLayout更改为1会更好。这很有帮助,唯一的问题是,TextView中的很长文本会使一些文本隐藏在图片后面,而不是以。。。在查看图像之前,我编辑了我的答案。使用singleLine=true而不是maxLines=1。这仍然不起作用。。我得到了一行你看到我的预览了吗?只需复制整段代码并替换为您的。尚未到家:将在今晚检查!谢谢
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/iv_pic"
android:gravity="start|center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:ellipsize="end"
android:focusable="false"
android:singleLine="true"
android:padding="2dp"
android:text="tv_exampleaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
android:textAlignment="center"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:ellipsize="end"
android:focusable="false"
android:inputType="number"
android:singleLine="true"
android:padding="2dp"
android:text="tv_example"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold|italic" />
</LinearLayout>
<ImageView
android:id="@+id/iv_pic"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:background="#ff0000" />
</RelativeLayout>