Android:带文字和图标的LinearLayout

时间:2016-04-05 13:56:11

标签: android android-layout

我有这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:azeoo="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/opening_today_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <TextView
        android:id="@+id/opening_today"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="A standard line" />

    <ImageView
        android:layout_toRightOf="@id/opening_today"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:paddingLeft="8dp"
        android:src="@drawable/ic_arrow_drop_down" />

</RelativeLayout>

</LinearLayout>

结果是:

enter image description here

但是,如果我为“opening_today”添加一个长文本,例如“非常非常非常长的行,并且有许多带有ellipsize end的文本”,ImageView就会消失:

enter image description here

那么,即使文字尺寸很大,我怎样才能保持 ImageView?

3 个答案:

答案 0 :(得分:2)

尝试这可能对您有所帮助

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:azeoo="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/opening_today_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="left"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <TextView
        android:id="@+id/opening_today"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:layout_toLeftOf="@+id/iv"
        android:maxLines="1"
        android:text="A very very very long line with a lot of texts with ellipsize end" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentRight="true"
        android:paddingLeft="8dp"
        android:src="@drawable/ic_arrow_drop_down" />
</RelativeLayout>

答案 1 :(得分:0)

试试这个

<RelativeLayout
        android:id="@+id/opening_today_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        >

        <TextView
            android:id="@+id/opening_today"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="A standard lffsdfsfsdfsfsfhgf fkhh gkjhg hkgj jhg hjgk jhg jkgjhg kjh jhgkj gkjsfsfsdfsdfsfsfdfdfdfsdf sdfsdfsdf sdfsdfsdfsdfsfdsdfsdfsfsfine"
            android:drawableRight="@drawable/ic_arrow_drop_down"
            />
    </RelativeLayout>

答案 2 :(得分:0)

正如 cricket_007 在评论中所说,您可以使用TextView

android:drawableRight代码中引用图片direclty
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:azeoo="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/opening_today_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <TextView
        android:id="@+id/opening_today"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="A standard line A standard line A standard line A standard line A standard line"
        android:drawableRight="@drawable/ic_arrow_drop_down"/>

</RelativeLayout>

执行此操作后,您无法再像初始代码那样指定图像的高度和宽度。请务必在所有res/drawable文件夹中保留正确的图片大小。

相关问题