[英]How to align accordingly an ImageView and a TextView in a custom ListView row?
我使用自定義適配器和自定義行布局(包括ImageView和TextView)創建了一個列表視圖。
我的問題是,由於在我的應用程序運行時以編程方式創建了視圖,因此,例如, 出現以下結果:
* 黑色矩形代表每行中的圖標
包含這些視圖的視圖是“線性布局”,我試圖做的是將重力設置為中心。 這行得通。 但是當涉及到長標題時,Icon將移出中間位置 。
無論標題長度如何,我如何才能成功獲得以下結果(盡管不夠大)?
提前致謝! 我的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>
這是不可能的,因為線性布局的重力為“ 中心 ”,您應將此線性布局的重力更改為“ 向左 ”,並在視圖的左側進行任何所需的邊距處理 。
去做:
<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>
試試看
<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"/>
將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.