[英]How to align accordingly an ImageView and a TextView in a custom ListView row?
I have created a list view with a custom adapter and custom row layout that includes an ImageView and a TextView. 我使用自定义适配器和自定义行布局(包括ImageView和TextView)创建了一个列表视图。
What my problem is, because of the views that are programatically created at my app run time this result comes up for ex: 我的问题是,由于在我的应用程序运行时以编程方式创建了视图,因此,例如, 出现以下结果:
* Black rectangles represent icons in each row * 黑色矩形代表每行中的图标
The view that includes these views is Linear Layout and what I tried to do is to set gravity to center. 包含这些视图的视图是“线性布局”,我试图做的是将重力设置为中心。 It work.
这行得通。 But when it comes to long titles the Icon moves out the middle .
但是当涉及到长标题时,Icon将移出中间位置 。
How can I succeed the following result,with whatever title length comes on(No huge enough though)? 无论标题长度如何,我如何才能成功获得以下结果(尽管不够大)?
Thanks in advance! 提前致谢! My xml file:
我的xml文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
<ImageView
android:id="@+id/menu_item_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="@+id/menu_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item_name"
android:textColor="#fff"
android:textSize="16sp"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
这是不可能的,因为线性布局的重力为“ 中心 ”,您应将此线性布局的重力更改为“ 向左 ”,并在视图的左侧进行任何所需的边距处理 。
Do That: 去做:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>
<ImageView
android:id="@+id/menu_item_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="@+id/menu_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item_name"
android:textColor="#fff"
android:textSize="16sp"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
Try this out 试试看
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="@+id/menu_item_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/menu_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item_name"
android:textColor="#fff"
android:textSize="16sp"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"/>
Change gravity
of LinearLayout
to left
, set up layout_marginLeft
of ImageView
将
LinearLayout
gravity
更改为left
,设置ImageView
layout_marginLeft
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="left">
<ImageView
android:id="@+id/menu_item_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="50dp"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.