android:如何向 FrameLayout 内的 LinearLayout 添加填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21592738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:47:23  来源:igfitidea点击:

android: how do I add padding to a LinearLayout inside a FrameLayout

androidandroid-layoutandroid-framelayout

提问by user3093402

I have a FrameLayout with many children. One of the children is a LinearLayout. I want the width of the LinearLayout to match_parentbut about 90% so; which means I want to add some sort of paddingLeft/Rightor margingLeft/Right. But when I add the padding or margin, it is not applied in the Graphical Layout. How do I do this correctly?

我有一个有很多孩子的 FrameLayout。其中一个孩子是 LinearLayout。我希望 LinearLayout 的宽度match_parent大约为 90%;这意味着我想添加某种paddingLeft/Rightor margingLeft/Right。但是当我添加填充或边距时,它不会应用于图形布局。我该如何正确地做到这一点?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:background="@color/red"
        android:marginLeft="20dp"
        android:marginRight="20dp"
        android:orientation="vertical" >
....

回答by Rat-a-tat-a-tat Ratatouille

you have

你有

android:marginLeft="20dp"
android:marginRight="20dp"

instead change it to:

而是将其更改为:

android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"

If it still doesnot work, put this layout inside another layout and set the margins to the other layout.

如果它仍然不起作用,请将这个布局放在另一个布局中并将边距设置为另一个布局。

回答by Namrata

You worte following code, which is not correct

您编写了以下代码,这是不正确的

android:marginLeft="20dp"
android:marginRight="20dp"

Try This :-

尝试这个 :-

  <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            **android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"**
            android:background="@android:color/white"
            android:orientation="vertical" >
        </LinearLayout>

    </FrameLayout>

Similarly you can give margin at Top and Bottom

同样,您可以在顶部和底部提供边距