我XML代码中ListView的行为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="2"
android:gravity="center" >
<ImageView
android:id="@+id/iv_image"
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/app_icon_17"
android:contentDescription="@string/empty" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="2"
android:gravity="center" >
<Button
android:id="@+id/btn_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/btn_delete_new"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
我希望最后一个id为btn_delete的按钮保持右对齐。在设计时,"图形布局"中显示的正是我想要的效果。但当我运行它时,在模拟器上无法实现这个效果。
请看输出:
![enter image description here](https://istack.dev59.com/kcFIz.webp)
RelativeLayout
或者FrameLayout
,它们都比LinearLayout
更好,你就可以得到你想要的效果。我更喜欢RelativeLayout
,因为它适用于这种类型的列表行设计。 - user370305