对准的LinearLayout内一个TextView和ImageView的 [英] Aligning a TextView and ImageView inside a LinearLayout

查看:156
本文介绍了对准的LinearLayout内一个TextView和ImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义适配器的ListView和这个列表项的布局。该项目的布局仅仅是一个与ImageView的TextView的。图像应对准到屏幕的右侧。但是,图像的位置移向了一下,取决于TextView的文本的长度。这是什么样子:

I have a ListView with custom adapter and a layout for items in this List. The layout for the items is just a TextView with an ImageView. The image should be aligned to the right side of the screen. However, the image's position is shifting a bit, depending on the length of the text in the TextView. This is what it looks like:

这是我的XML code:

This is my XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/txtCategoryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:singleLine="true"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textColor="@color/list_text_color"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imgArrowRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/right_arrow_44"
        android:contentDescription="@string/img_description_right_arrow"
        android:layout_gravity="right" />

</LinearLayout>

我如何可以使图像始终靠右对齐,而不被碰一碰?

How can I make the image be always aligned to the right, without being nudged?

推荐答案

RelativeLayout的应解决用精美您的问题的android:layout_alignParentRight 的android:layout_alignParentLeft 。是这样的:

RelativeLayout should solve your problem nicely using android:layout_alignParentRight and android:layout_alignParentLeft. Something like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/txtCategoryName"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="end"
    android:gravity="center_vertical"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:textColor="@color/list_text_color"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imgArrowRight"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/right_arrow_44"
    android:contentDescription="@string/img_description_right_arrow"/>

这篇关于对准的LinearLayout内一个TextView和ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆