具有layout_weights的LinearLayout以及具有不同高度的子级

发布于 2025-01-01 10:07:11 字数 1334 浏览 3 评论 0原文

我的图像按钮高度为 40 像素,但我的画廊高度要大得多。现在,我的图像按钮占据了它们所获得的整个空间,并且与我的画廊高度相同。有没有办法在保持这种水平布局的同时保持按钮的原始大小?

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingTop="20dp" >

        <ImageButton
            android:id="@+id/left_arrow"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shared_arrow_left_button_selector"
            android:scaleType="fitCenter" />

        <Gallery
            android:id="@+id/gallery"
            android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:spacing="15dp" />

        <ImageButton
            android:id="@+id/right_arrow"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shared_arrow_right_button_selector"
            android:scaleType="fitCenter" />
    </LinearLayout>

My image button is 40px in height, but my gallery is a lot bigger in height. Right now, my imagebuttons take up the entire space they are given, and is the same height as my gallery. Is there anway to keep the buttons their original size while keeping this horizontal layout?

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingTop="20dp" >

        <ImageButton
            android:id="@+id/left_arrow"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shared_arrow_left_button_selector"
            android:scaleType="fitCenter" />

        <Gallery
            android:id="@+id/gallery"
            android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:spacing="15dp" />

        <ImageButton
            android:id="@+id/right_arrow"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shared_arrow_right_button_selector"
            android:scaleType="fitCenter" />
    </LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

踏月而来 2025-01-08 10:07:11

您正在应用

android:layout_width="match_parent"
android:layout_height="match_parent"

两个 ImageButton,以便它们填充高度,如果您只想让 ImageButton 包裹其原始大小,只需将它们更改为 wrap_content 并删除 layout_weight 属性。此外,当您将 layout_weight="1" 应用于图库时,只需更改 android:layout_width="0dp" ,因为宽度将由 layout_weight 管理> 财产本身。你的最终布局应该是这样的,

<?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:gravity="center"
    android:orientation="horizontal"
    android:paddingTop="20dp" >

    <ImageButton
        android:id="@+id/left_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter" />

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:spacing="15dp" />

    <ImageButton
        android:id="@+id/right_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter" />

</LinearLayout>

You are applying

android:layout_width="match_parent"
android:layout_height="match_parent"

to both the ImageButtons so they are filling the height, if you just want ImageButtons to wrap their original size just change them to wrap_content and remove the layout_weight property. Also when you are applying layout_weight="1" to gallery just change android:layout_width="0dp" as the width will be managed by the layout_weight property itself. Your final layout should be like,

<?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:gravity="center"
    android:orientation="horizontal"
    android:paddingTop="20dp" >

    <ImageButton
        android:id="@+id/left_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter" />

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:spacing="15dp" />

    <ImageButton
        android:id="@+id/right_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter" />

</LinearLayout>
冷夜 2025-01-08 10:07:11
<ImageButton
        android:id="@+id/left_arrow"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shared_arrow_left_button_selector"
        android:scaleType="centerinside" />
Remove Weight attribute, and set width and height of buttons to wrap_content
    <Gallery
        android:id="@+id/gallery"
        android:focusable="true"
    android:focusableInTouchMode="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spacing="15dp" />

    <ImageButton
        android:id="@+id/right_arrow"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shared_arrow_right_button_selector"
        android:scaleType="centerinside" />
</LinearLayout>
<ImageButton
        android:id="@+id/left_arrow"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shared_arrow_left_button_selector"
        android:scaleType="centerinside" />
Remove Weight attribute, and set width and height of buttons to wrap_content
    <Gallery
        android:id="@+id/gallery"
        android:focusable="true"
    android:focusableInTouchMode="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spacing="15dp" />

    <ImageButton
        android:id="@+id/right_arrow"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shared_arrow_right_button_selector"
        android:scaleType="centerinside" />
</LinearLayout>
酒几许 2025-01-08 10:07:11

删除 ImageButtons 中的布局权重 = 3,这是导致显示的图像按钮大于 Gallery 的原因。

我认为你不知道如何使用layout_weight属性,所以你不知道如何解决这个问题。首先了解layout_weight。

如果您有任何疑问,请随时问我。

在图像按钮和图库中使用下面的行,会很好。

android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"

Remove the layout weight = 3 in ImageButtons, it is the cause to show image button bigger than Gallery.

I think you don't know how to use layout_weight property, so that you don't know how to solve this problem. First know about layout_weight.

If you have any doubt feel free to ask me.

Use below lines in both image buttons and gallery, it will be nice.

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