Android:布局列表标题
我有一个包含两个图像按钮的列表标题。我希望其中一个图像按钮位于左侧,另一个位于右侧。这就是我的 xml 文件现在的样子,但它不起作用,它只是将左侧的两个图像按钮放在一起。我也尝试过不使用额外的 LinearLayouts,但没有成功。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="fill_horizontal"
android:background="@drawable/list_header_background">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="left"
android:background="@drawable/list_header_background">
<ImageButton
android:id="@+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="@drawable/ic_refresh"
android:background="@drawable/list_header_background"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="right"
android:background="@drawable/list_header_background">
<ImageButton
android:id="@+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="@drawable/ic_battle"
android:background="@drawable/list_header_background"
/>
</LinearLayout>
</LinearLayout>
I have a header for a list that contains two image buttons. I want one of those image buttons to be on the left, and the other to be on the right. This is what my xml file looks like right now but it does not work, it just puts the two image buttons on the left together. I have also tried without the additional LinearLayouts but no luck.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="fill_horizontal"
android:background="@drawable/list_header_background">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="left"
android:background="@drawable/list_header_background">
<ImageButton
android:id="@+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="@drawable/ic_refresh"
android:background="@drawable/list_header_background"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="right"
android:background="@drawable/list_header_background">
<ImageButton
android:id="@+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="@drawable/ic_battle"
android:background="@drawable/list_header_background"
/>
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 kabuko 所写,
RelativeLayout
宽度等于父宽度。当我们在该RelativeLayout
中有一个子视图时,我们可以使用android:layout_alignParentXXXXXX="true"
参数来将其(子视图)相应地与父视图对齐。As kabuko wrote, the
RelativeLayout
width is equal to the parent width. When we have a child view inside thatRelativeLayout
, we can use theandroid:layout_alignParentXXXXXX="true"
parameter in order to align it (the child) accordingly to the parent.您可能希望在按钮上使用带有layout_alignParentLeft 和layout_alignParentRight 的RelativeLayout。
You probably want to use a RelativeLayout with layout_alignParentLeft and layout_alignParentRight on the buttons.