Android 如何正确放置视图以使线性布局内的所有视图可见?

Android 如何正确放置视图以使线性布局内的所有视图可见?,android,layout,Android,Layout,我有一个水平线性布局,包含3个元素、一个图标和两个文本视图: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="20dp" android:layout_he

我有一个水平线性布局,包含3个元素、一个图标和两个文本视图:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="8dp"
        android:background="@drawable/icon_place" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:maxLines="1"
        android:text="Information 1"
        android:textColor="@android:color/black"
        android:textSize="14sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Information 2"
        android:gravity="right"
        android:textSize="13sp"
        android:maxLines="1"
        android:textColor="@android:color/darker_gray"
        android:layout_margin="8dp"/>

</LinearLayout>

给我这个:

我希望第二个文本视图“信息2”始终保持可见,如果需要,第一个文本视图将被裁剪。但在第一个文本视图中,长文本会发生以下情况:


那么我应该怎么做呢?谢谢你的帮助

如果将其添加到所有3个组件中,则有一个“android:weigth=1”的概念。然后,它们的宽度将始终为33.3%,并且在显示长文本时,宽度不应改变,如果您希望宽度约为10%40%40%,则必须使用此标记,ksatai值不接受0.5格式,他们需要在此以这种格式写入。5

您应该设置
android:layout\u weight=“1”中间的“代码>文本视图< /代码>,并更改<代码> Matkh Prime<代码> > <代码> WrAPX内容< /代码>第三代码>文本视图< /代码> ./P>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="8dp"
        android:background="@drawable/ic_launcher_background" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_weight="1"
        android:maxLines="1"
        android:text="Information 1"
        android:textColor="@android:color/black"
        android:textSize="14sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:gravity="right"
        android:maxLines="1"
        android:text="Information 2"
        android:textColor="@android:color/darker_gray"
        android:textSize="13sp" />

</LinearLayout>

使用这个-

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_margin="8dp"
            android:background="@android:drawable/ic_lock_idle_alarm" />

        <TextView
            android:layout_toRightOf="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:maxLines="1"
            android:text="Information 1"
            android:textColor="@android:color/black"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Information 2"
            android:layout_marginLeft="28dp"
            android:background="@android:color/white"
            android:gravity="right"
            android:textSize="13sp"
            android:maxLines="1"
            android:textColor="@android:color/darker_gray"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"/>

    </RelativeLayout>

您可以使用weightsum属性将两个文本视图包装成水平直线布局,将其拆分为两个大小相等的部分。如果文本太长,也可以将其省略

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="8dp"
        android:background="@drawable/icon_place" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:maxLines="1"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:text="Information 1 with a very very long test"
            android:ellipsize="end"
            android:textColor="@android:color/black"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Information 2"
            android:gravity="center_horizontal"
            android:textSize="13sp"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/darker_gray"
            android:layout_margin="8dp"/>

    </LinearLayout>

</LinearLayout>

结果是:


首先要设置android:maxLines=1,使文本视图在一行中可见。如果您希望文本视图根据内容自动裁剪。从xml中删除以下代码

android:maxLines="1"
第二件事您的第一个
textview
获取线性布局的剩余空间,而不是第二个
textview
imageview
必须使用linearlayout的android:weight属性。您可以根据需要在此处查看“简单布局权重”属性。

Weightsum
equalweights
分布式权重
复杂线性布局所需的全部内容。请参阅本教程以获取帮助

这是密码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">


<ImageView
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_margin="8dp"
    android:background="@drawable/ic_menu_send" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:layout_weight="1"
    android:text="Information 1 IInformation Information InformationInformationvvvInformation Information"
    android:textColor="@android:color/black"
    android:textSize="14sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Information 2"
    android:textSize="13sp"
    android:maxLines="1"
    android:textColor="@android:color/darker_gray"
    android:layout_margin="8dp"/>

</LinearLayout>

输出:

在编辑器视图中


非常感谢,您的解决方案很有效。PS:我保留了
maxLines=1
,因为我不想为这个文本视图显示多行,而且它可以正常工作,所以它可以正确显示,而不会裁剪另一行。欢迎。很乐意帮忙。!