I have created a list view with a custom adapter and custom row layout that includes an ImageView and a 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 .
How can I succeed the following result,with whatever title length comes on(No huge enough though)?
Thanks in advance! My xml file:
<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
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.