首页 > 解决方案 > Layout display so wrongly

问题描述

The layout will all move down in the display ,as if the view should be on the top,but i run it on AVD and it moves down to the bottom,so there's a whole blank area on top of that It's just this layout and any other layout is fine . I don't know why it happens .But i think the reason it's either XML or JAVA.

(I use a layout to open this layout and it's in fragment )

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:contentDescription="@string/todo"
            android:src="@drawable/layout" />
</RelativeLayout>

JAVA

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        tranView = inflater.inflate(R.layout.setting,container,false);
        LinearLayout app_layer = (LinearLayout) tranView.findViewById(R.id.layout);

        app_layer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment fragment = null;
                switch (view.getId()) {
                    case R.id.layout:
                        fragment = new Page_Aboutus();
                        replaceFragment(fragment);
                        break;

                }
            }
        });
        return tranView;
    }


    public void replaceFragment(Fragment somefragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, somefragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

标签: androidandroid-layout

解决方案


对于 a RelativeLayout,您指定某个元素应与其父元素的顶部对齐android:layout_alignParentTop="true"

所以,使用:

<ImageView
    android:id="@+id/imageView6"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/todo"
    android:src="@drawable/layout" />

推荐阅读