Android:布局列表标题

发布于 2024-12-18 22:46:51 字数 1810 浏览 6 评论 0原文

我有一个包含两个图像按钮的列表标题。我希望其中一个图像按钮位于左侧,另一个位于右侧。这就是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠北森屋 2024-12-25 22:46:51

正如 kabuko 所写,RelativeLayout 宽度等于父宽度。当我们在该 RelativeLayout 中有一个子视图时,我们可以使用 android:layout_alignParentXXXXXX="true" 参数来将其(子视图)相应地与父视图对齐。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  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:layout_alignParentLeft="true"
      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:layout_alignParentRight="true"
      android:background="@drawable/list_header_background"
   />
</RelativeLayout>

As kabuko wrote, the RelativeLayout width is equal to the parent width. When we have a child view inside that RelativeLayout, we can use the android:layout_alignParentXXXXXX="true" parameter in order to align it (the child) accordingly to the parent.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  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:layout_alignParentLeft="true"
      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:layout_alignParentRight="true"
      android:background="@drawable/list_header_background"
   />
</RelativeLayout>
电影里的梦 2024-12-25 22:46:51

您可能希望在按钮上使用带有layout_alignParentLeft 和layout_alignParentRight 的RelativeLayout。

You probably want to use a RelativeLayout with layout_alignParentLeft and layout_alignParentRight on the buttons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文