布局显示错误

时间:2019-04-01 11:42:52

标签: android android-layout

布局将在显示屏中全部向下移动,就像视图应该在顶部一样,但是我在AVD上运行它,并且它向下移动到底部,因此顶部有一个整个空白区域 只是这个布局,其他任何布局都可以。我不知道为什么会发生。但是我认为原因是XML或JAVA。

(我使用布局打开此布局,它位于片段中)

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

1 个答案:

答案 0 :(得分:2)

对于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" />
相关问题