问题 FrameLayout中的'android:foreground'和'android:foregroundGravity'如何影响其外观?


FrameLayout具有属性 android:foregroundandroid:foregroundGravity 和 android:measureAllChildren。 我已经尝试过这些属性,但无法弄清楚它们如何影响布局的外观或它们的工作方式。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:foreground="@android:color/holo_blue_dark"
 android:foregroundGravity="center"
 android:measureAllChildren="true"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.example.framelayoutdemo.MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/btn_dialog" />

</FrameLayout>

我用谷歌搜索,但无法找到这些属性的帮助。 请给我一个链接,我可以参考或帮助我举个例子。 谢谢


10550
2018-01-26 20:09


起源



答案:


android:foreground 指向你的前景 Framelayout 需要在所有孩子之上画画

android:foregroundGravity 如果该资产是可绘制的,则指向该资产的严重性。这意味着如果你正在使用

android:foreground="@android:drawable/bottom_bar"

然后你的drawable被绘制在它的所有孩子的顶部,它位于the的中心 Framelayout

android:measureAllChildren : 确定是测量所有孩子还是仅测量孩子 VISIBLE 要么 INVISIBLE 测量时的状态。默认为false。这意味着,如果将其设置为false,则表示所有元素 GONE 当国家没有考虑到 Framelayout测量阶段正在进行中。

希望这能回答你的问题。


9
2018-01-26 20:31



解释 android:foreground 和 android:foregroundGravity 是完美的,但我没有得到如何使用 android:measureAllChildren :或者为什么要使用。如果您能为其工作提供和示例,那将是一个很大的帮助。非常感谢。 - SimplyProgrammer
android:measureAllChildren 绝对没有影响这个布局。 - j__m


答案:


android:foreground 指向你的前景 Framelayout 需要在所有孩子之上画画

android:foregroundGravity 如果该资产是可绘制的,则指向该资产的严重性。这意味着如果你正在使用

android:foreground="@android:drawable/bottom_bar"

然后你的drawable被绘制在它的所有孩子的顶部,它位于the的中心 Framelayout

android:measureAllChildren : 确定是测量所有孩子还是仅测量孩子 VISIBLE 要么 INVISIBLE 测量时的状态。默认为false。这意味着,如果将其设置为false,则表示所有元素 GONE 当国家没有考虑到 Framelayout测量阶段正在进行中。

希望这能回答你的问题。


9
2018-01-26 20:31



解释 android:foreground 和 android:foregroundGravity 是完美的,但我没有得到如何使用 android:measureAllChildren :或者为什么要使用。如果您能为其工作提供和示例,那将是一个很大的帮助。非常感谢。 - SimplyProgrammer
android:measureAllChildren 绝对没有影响这个布局。 - j__m


如果measureallchildren设置为“true”,那么即使视图可见性处于消失状态,它也将显示帧布局的实际宽度和高度。 例如

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/myframe"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:measureAllChildren="true"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:src="@drawable/ic_launcher"/>

</FrameLayout>

下面是MainActivity.java的代码。如果我们使用Toast在屏幕上显示高度和宽度。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myframe_activity);
        FrameLayout frame=(FrameLayout)findViewById(R.id.frame);
        frame.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int width = frame.getMeasuredWidth();
        int height = frame.getMeasuredHeight();
        Toast.makeText(getApplicationContext(),"width="+width+"  height="+height,Toast.LENGTH_SHORT).show();

    }

}

如果我们在模拟器中运行App,则Toast消息形式的输出将如下所示: width = 72 height = 72

现在,如果我们将measureAllChildren的值更改为false,那么Toast消息输出框架布局的宽度和高度将为: width = 0 height = 0


2
2017-10-02 06:52