your programing

FrameLayout 여백이 작동하지 않습니다.

lovepro 2020. 10. 15. 08:19
반응형

FrameLayout 여백이 작동하지 않습니다.


내 레이아웃 구조는 다음과 같습니다.

LinearLayout
    FrameLayout
       ImageView
       ImageView
    FrameLayout
    TextView
LinearLayout

FrameLayout 안에있는 두 개의 ImageView에 대한 여백을 설정했습니다. 그러나 FrameLayout 여백은 삭제되고 항상 Image가 왼쪽 상단 모서리로 설정됩니다. FrameLayout에서 LinearLayout으로 변경하면 여백이 제대로 작동합니다. 이것을 처리하는 방법?

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/inner1"
    >
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
        >

            <ImageView
             android:layout_width="24px" 
             android:layout_height="24px" 
             android:id="@+id/favicon"
             android:layout_marginLeft="50px"
             android:layout_marginTop="50px"
             android:layout_marginBottom="40px"
             android:layout_marginRight="70px"      

            />      
            <ImageView
             android:layout_width="52px" 
             android:layout_height="52px" 
             android:id="@+id/applefavicon" 
             android:layout_marginLeft="100px"
             android:layout_marginTop="100px"
             android:layout_marginBottom="100px"
             android:layout_marginRight="100px"              
            />

        </FrameLayout>  
            <TextView
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/title"                 
            android:layout_marginLeft="10px"        
            android:layout_marginTop="20px"
            android:textColor="#FFFFFF"  
            android:textSize = "15px"
            android:singleLine = "true"
            />

    </LinearLayout>

나도 같은 문제가 있었고 layout_ImageView의 레이아웃 중력, 즉 android:layout_gravity="top"XML 리소스 파일이나 코드에서 설정할 때까지 여백 설정이 작동하지 않는다는 것을 알았습니다 FrameLayout.LayoutParams.gravity = Gravity.TOP;.


이유를 더 명확히하기 위해서. FrameLayout.onLayout () 호출에는 다음이 포함됩니다 (최소한 api v2.3.4에서).

    // for each child
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int gravity = lp.gravity;
    if (gravity != -1) {
        // handle all margin related stuff

따라서 중력이 -1이면 여백 계산이 없습니다. 그리고 문제는 FrameLayout.LayoutParams의 중력은 다음과 같이 정의됩니다.

    gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1);

따라서 중력이 설정되지 않으면 여백 계산이 없습니다.


add your xml this attribute and re run

android:layout_gravity="top"

everything is Ok!

and you dont set new layout params like this;

FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(WallpapersActivity.ScreenWidth/2, layH);

use like this:

FrameLayout.LayoutParams llp = (LayoutParams) myFrameLay.getLayoutParams();
llp.height = 100;
llp.width = 100;
myFrameLay.setLayoutParams(llp);

Taken from the FrameLayout docs (link)

The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits).

This seems to describe the fact that it'll strip margins out. Like boulder mentioned, you could try switching to padding as it can be used to produce a similar effect if done properly.

Out of curiosity, you mentioned that it does work fine when using a LinearLayout container, why the choice of FrameLayout?


Have you tried android:layout_gravity ? Try using android:padding in you ImageViews instead of android:layout_margin. AFAIK margins doesn't work properly on Frame layout. I even had to write custom layout for that purpose once. BTW, how do you want allign you ImageViews?


try setCropToPadding(true) to ImageView ,this should be helped!


you have to set your ImageView's layout gravity top i.e. android:layout_gravity="top" in the XML resource file, or from code: FrameLayout.LayoutParams.gravity = Gravity.TOP

참고URL : https://stackoverflow.com/questions/5401952/framelayout-margin-not-working

반응형