如何将ImageView与设置宽度文本字段对齐

时间:2015-03-09 19:08:00

标签: android

我在尝试让这种布局工作时遇到很多问题。在我的列表中,我想要带有名称(TextView),图标(ImageView)和状态(TextView)的行。但我希望状态是右对齐和固定字符宽度(20),以及与状态高度相匹配的图标,并且在左侧。这样,当我有很多行时,无论状态如何,图标都会对齐。

desired layout

我无法通过任何布局来实现这一目标,我已尝试将状态minEms设置为20,但由于某种原因它会忽略重力。这是我到目前为止所拥有的内容,但这样图标就不会对齐,因为无论字符数是多少,它们都会保持在状态的左侧。内部LinearLayout的原因是我可以将ImageView的高度设置为match_parent,并且与文本的高度相同。

<LinearLayout
    android:id="@+id/row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing a really really really really really long name"
        android:ellipsize="end"
        android:singleLine="true"/>

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

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/ic_status" />

        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is max txt size" />
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

试试这个布局。

更改父级的高度以更改子视图高度。

另请注意weightSum =“100” 名称TextView的布局权重设置为70,因此占用屏幕宽度的70%。做出你想要的任何改变,只要确保孩子的总重量加起来为weightSum。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="10" >

<TextView
    android:id="@+id/name"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="7"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="testing a really really really really really long name" />

<ImageView
    android:id="@+id/icon"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:src="@drawable/ic_status" />

<TextView
    android:id="@+id/status"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:text="This is max txt size"
    android:textColor="#FFFFFF" />

</LinearLayout>
相关问题