Android布局宽度&布局高度,它是如何工作的?

发布于 2024-12-23 15:15:32 字数 2675 浏览 1 评论 0原文

我正在尝试创建一个界面,其中将向用户显示两个或多个按钮,如果单击按钮,则会向他显示一些布局。我正在使用 SlidingDrawer 来实现此目的。

好吧,我对 layout_width & 感到困惑。布局高度属性

如果我设置如下属性,则屏幕上仅显示“Handle 1”。

android:layout_width="fill_parent" android:layout_height="wrap_content"

老实说,我对这两个属性都没有足够的了解。 有人可以分享他对它们的了解吗?

ma​​in.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <SlidingDrawer android:id="@+id/slidingDrawer1"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:handle="@+id/handle1"
            android:content="@+id/content1">
                <Button android:text="Handle 1" android:layout_height="wrap_content"
                        android:layout_width="fill_parent" android:id="@+id/handle1"></Button>
                <LinearLayout android:id="@+id/content1"
                        android:layout_width="fill_parent" android:layout_height="wrap_content"
                        android:orientation="vertical" android:gravity="center"
                        android:background="#FF444444">
                        <Button android:text="Handle 1 Item 1" android:layout_height="wrap_content"
                                android:layout_width="wrap_content" android:id="@+id/item1"></Button>
                </LinearLayout>
    </SlidingDrawer>

    <SlidingDrawer android:id="@+id/slidingDrawer2"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:handle="@+id/handle2"
            android:content="@+id/content2">
                <Button android:text="Handle 2" android:layout_height="wrap_content"
                        android:layout_width="fill_parent" android:id="@+id/handle2"></Button>
                <LinearLayout android:id="@+id/content2"
                        android:layout_width="fill_parent" android:layout_height="wrap_content"
                        android:orientation="vertical" android:gravity="center"
                        android:background="#FF444444">
                        <Button android:text="Handle 2 Item 1" android:layout_height="wrap_content"
                                android:layout_width="wrap_content" android:id="@+id/item2"></Button>
                </LinearLayout>
    </SlidingDrawer>     
</LinearLayout>

I am trying to create an interface, where two or more buttons will be displayed to the user, if a button is clicked, some Layouts will be displayed to him. I am using the SlidingDrawer for this purpose.

Well, i am confused by layout_width & layout_height properties.

If i set the properties like below only the "Handle 1" is shown on the screen.

android:layout_width="fill_parent" android:layout_height="wrap_content"

Honestly saying, i don't have enough knowledge about both of these properties. Can someone please share his knowledge about them?

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <SlidingDrawer android:id="@+id/slidingDrawer1"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:handle="@+id/handle1"
            android:content="@+id/content1">
                <Button android:text="Handle 1" android:layout_height="wrap_content"
                        android:layout_width="fill_parent" android:id="@+id/handle1"></Button>
                <LinearLayout android:id="@+id/content1"
                        android:layout_width="fill_parent" android:layout_height="wrap_content"
                        android:orientation="vertical" android:gravity="center"
                        android:background="#FF444444">
                        <Button android:text="Handle 1 Item 1" android:layout_height="wrap_content"
                                android:layout_width="wrap_content" android:id="@+id/item1"></Button>
                </LinearLayout>
    </SlidingDrawer>

    <SlidingDrawer android:id="@+id/slidingDrawer2"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:handle="@+id/handle2"
            android:content="@+id/content2">
                <Button android:text="Handle 2" android:layout_height="wrap_content"
                        android:layout_width="fill_parent" android:id="@+id/handle2"></Button>
                <LinearLayout android:id="@+id/content2"
                        android:layout_width="fill_parent" android:layout_height="wrap_content"
                        android:orientation="vertical" android:gravity="center"
                        android:background="#FF444444">
                        <Button android:text="Handle 2 Item 1" android:layout_height="wrap_content"
                                android:layout_width="wrap_content" android:id="@+id/item2"></Button>
                </LinearLayout>
    </SlidingDrawer>     
</LinearLayout>

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

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

发布评论

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

评论(3

蝶…霜飞 2024-12-30 15:15:33

fill_parent = match_parent,这意味着它采用“父”容器的宽度或高度(它被指定为哪个属性)

wrapp_content意味着高度或宽度采用

您想要的 “子”容器的高度或宽度使用权重而不是通常使用match_parent或wrap_content。

当您确实使用权重时,您希望将宽度设置为 0dp。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <LinearLayout
    android:id="@+id/lowerLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0" >
            <LinearLayout
            android:id="@+id/headerLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight=".7" >
                <Button
                    android:id="@+id/previousMonthButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Previous" />

                <TextView
                    android:id="@+id/monthName"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:text="Large Text"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_horizontal" />

                <Button
                    android:id="@+id/nextMonthButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Next" />

            </LinearLayout>

            <LinearLayout
            android:id="@+id/lowerLayout3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight=".3" >

                <Button
                    android:id="@+id/addEvent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

    </LinearLayout>
    <LinearLayout
    android:id="@+id/lowerLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0" >

        <LinearLayout
            android:id="@+id/monthView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight=".7" >

        </LinearLayout>

        <ListView
            android:id="@+id/listView1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".3" >
        </ListView>
    </LinearLayout>

</LinearLayout>

fill_parent = match_parent, and it means that it takes the width or height (which ever property it is being specified as) of the "parent" container

wrap_content means that the height or width takes on the height or width of the "child"

You want to use weights instead of using match_parent or wrap_content typically.

When you do use weights you want to set the width to 0dp.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <LinearLayout
    android:id="@+id/lowerLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0" >
            <LinearLayout
            android:id="@+id/headerLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight=".7" >
                <Button
                    android:id="@+id/previousMonthButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Previous" />

                <TextView
                    android:id="@+id/monthName"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:text="Large Text"
                    android:layout_weight="1"
                    android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_horizontal" />

                <Button
                    android:id="@+id/nextMonthButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Next" />

            </LinearLayout>

            <LinearLayout
            android:id="@+id/lowerLayout3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight=".3" >

                <Button
                    android:id="@+id/addEvent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

    </LinearLayout>
    <LinearLayout
    android:id="@+id/lowerLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0" >

        <LinearLayout
            android:id="@+id/monthView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight=".7" >

        </LinearLayout>

        <ListView
            android:id="@+id/listView1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".3" >
        </ListView>
    </LinearLayout>

</LinearLayout>
热风软妹 2024-12-30 15:15:33

我认为您正在尝试添加 2 个滑动抽屉。

SlidingDrawer 的大小定义了内容滑出后将占用多少空间,因此 SlidingDrawer 通常应该对其两个维度使用 match_parent 在这种情况下,一个维度与另一个维度重叠,这样您就看不到处理程序 2 。

如果您将第一个 SlidingDrawer 的可见性设置为“消失”,您可以看到另一个 SlidingDrawer 。

更新

嗨,

请尝试此代码可能会对您有所帮助

<?xml version="1.0" encoding="utf-8"?>

<SlidingDrawer
    android:id="@+id/slidingDrawer1"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:content="@+id/content1"
    android:handle="@+id/handle1" >

    <Button
        android:id="@+id/handle1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Handle 1" >
    </Button>

    <LinearLayout
        android:id="@+id/content1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FF444444"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/item1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Handle 1 Item 1" >
        </Button>
    </LinearLayout>
</SlidingDrawer>

I think you trying to add 2 Sliding Drawer.

The size of the SlidingDrawer defines how much space the content will occupy once slid out so SlidingDrawer should usually use match_parent for both its dimensionsin this case one overlap other one so you can't see the handler 2 .

If you set visibility "gone" of first SlidingDrawer you can see the Other one .

Update

Hi ,

Please try this code may it help you

<?xml version="1.0" encoding="utf-8"?>

<SlidingDrawer
    android:id="@+id/slidingDrawer1"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:content="@+id/content1"
    android:handle="@+id/handle1" >

    <Button
        android:id="@+id/handle1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Handle 1" >
    </Button>

    <LinearLayout
        android:id="@+id/content1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FF444444"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/item1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Handle 1 Item 1" >
        </Button>
    </LinearLayout>
</SlidingDrawer>
杀手六號 2024-12-30 15:15:32

视图的 layout_widthlayout_height 属性旨在由其父容器使用。有些容器会忽略其中一个或两个;最尊敬他们。您需要查阅容器的文档(在您的情况下为 SlidingDrawer)以了解如何使用这些值。

您没有显示完整的 main.xml,因此很难确切地说出了什么问题。如果您发布问题的图片也会有所帮助。

编辑

在看到完整的布局后,我认为这里的基本问题是您使用 LinearLayout 来包含 SlidingDrawers。正如 SlidingDrawer 文档 所指出的,它们需要位于 FrameLayout 或relativelayout 中(实际上,任何允许多个视图相互叠加的容器)。

另一种可能性是第二个 SlidingDrawer 直接放置在第一个 SlidingDrawer 的下方。尝试更改第二个按钮的大小(例如,使文本更长)并查看它是否从按钮 1 的两侧突出。

The layout_width and layout_height properties of a view are intended to be used by its parent container. Some containers ignore one or both of these; most honor them. You need to consult the documentation of the container (in your case, SlidingDrawer) to understand how the values will be used.

You don't show the complete main.xml, so it's hard to say exactly what's going wrong. It would also help if you posted an image of what's wrong.

EDIT

After seeing your complete layout, I think that the basic problem here is that you are using a LinearLayout to contain the SlidingDrawers. As the docs for SlidingDrawer note, they need to be in either a FrameLayout or a RelativeLayout (actually, any container that allows multiple views to sit on top of one another).

Another possibility is that the second SlidingDrawer is being placed directly under the first one. Try changing the size of the second button (e.g., make the text longer) and see if it pokes out on either side of button 1.

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