您好,我设法使图标中心与线性 View 中的文本在同一行,但我想在右侧添加一个箭头图像,所以我必须切换到相对 View 。如图所示,我无法将相对 View 左侧图标设置为线性 View 。
编辑
我想让箭头图标留在右边。
左图是线性的,右图是相对的。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layercontainer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/categoryIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="@+id/categoryTitle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/categoryIcon"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<ImageView
android:id="@+id/nextIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
最佳答案
我建议使用 TextView 属性 drawableLeft和drawableRight
android:drawableLeft="@drawable/whatever"
android:drawableRight="@drawable/arrow"
android:width="fill_parent"
这样并设置 TextView 的重力居中,一切都应该没问题
此外,使用单个 TextView 而不是 RelativeLayout + 3 个 View 在简单性方面有了很大的改进。无论如何,如果您要坚持使用 3 个 View ,使用 LinearLayout 而不是 RelativeLayout 是一个很好的建议。
关于Android 相对布局无法使图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10075475/