Android Studio 项目:UI 底部被切断 [英] Android Studio Project: bottom of UI is cut off

查看:15
本文介绍了Android Studio 项目:UI 底部被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将应用程序加载到手机上或使用模拟器时,我的 ListView 和 ImageButton 的底部出现问题.我可以只使用边距或填充,但这不是特定于设备的吗?无论屏幕大小如何,我都希望我的应用程序看起来像我想要的那样.这是我的代码:

I'm having a problem with the bottom of my ListView and ImageButton being cut off when I load my app onto a phone or when using an emulator. I could just use margins or padding, but wouldn't that be device specific? I'd like my app to look as I want it to regardless of screen size. Here is my code:

班级:

public class Cookbook extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        String[] items = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
                        R.layout.row_layout,
                        items);

        View view = inflater.inflate(R.layout.frag_cookbook, container, false);
        ListView list = (ListView) view.findViewById(R.id.listView);
        list.setAdapter(adapter);


        return view;
    }
}

布局 XML:

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

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="fill"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:background="#f9e48f" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        android:background="@null"
        android:layout_gravity="end|bottom"
        android:layout_alignParentBottom="true"
        android:baselineAligned="false"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="1dp" />


</RelativeLayout>

Android Studio 预览如下所示:

The Android Studio preview looks like this:

但是当模拟它或将它加载到手机上时,它看起来像这样:

But when emulating it or loading it onto a phone, it looks like this:

推荐答案

这是因为您将 CoordinatorLayoutListView 一起使用.您可以将您的实现更改为 RecyclerView 以实现正确滚动.

This is because you are using CoordinatorLayout with ListView. You can change your implementation to RecyclerView to achieve correct scroll.

或者如果你是 5.0 以上的 tagetting,你可以使用下面的一段代码

or If you are tagetting above 5.0, you can use the following piece of code

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {listView.setNestedScrollingEnabled(true);}

我认为 CoordinatorLayout 只适用于 NestedScrollingChild.

I think CoordinatorLayout only works with the children of NestedScrollingChild.

这篇关于Android Studio 项目:UI 底部被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆