How do 'android:foreground' and 'android:foregroundGravity' in FrameLayout affect its appearance?
Asked Answered
D

2

12

FrameLayout has the attributes android:foreground, android:foregroundGravity and android:measureAllChildren. I have tried these attributes, but couldn't make out how they affect the layout's appearance or how they work.

<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>

I have googled, but couldn't find a help with these attributes. Please provide me a link which I can refer or help me with an example. Thanks

Detumescence answered 26/1, 2016 at 20:9 Comment(0)
T
18

android:foreground points to the foreground that your Framelayout needs to draw on top of all its children

android:foregroundGravity points to the gravity of that asset, if that asset is a drawable. That means if you are using

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

Then your drawable is drawn on top of all its children and it is positioned in the center of the Framelayout

android:measureAllChildren : Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false. That means, if it is set to false, then all the elements in the GONE state are not taken into consideration when the Framelayout's measure phase is going around.

Hope that answers your question.

Thais answered 26/1, 2016 at 20:31 Comment(3)
Explanation for android:foreground and android:foregroundGravity is perfect, but I didnt get how to use android:measureAllChildren :, or why to use. It will be a great help if you can provide and example for its working. Thanks a lot.Detumescence
android:measureAllChildren definitely has no effect on this layout.Acosmism
@Acosmism I think android:measureAllChildren is used only for ViewAnimator and ViewSwitcherStumer
P
3

If measureallchildren is set to "true" then it will show actual width and height of the frame layout even if the views visibility is in gone state. For example

<?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>

Below is the code of MainActivity.java . if we use Toast to display height and width on screen.

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();

    }

}

if we run the App in the Emulator the output in the form of Toast message will be like this: width=72 height=72

Now if we change the value of measureAllChildren to false then Toast message output frame layout width and height will be: width=0 height=0

Paleopsychology answered 2/10, 2017 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.