Android Studio 项目:UI 的底部被截断

Android Studio Project: bottom of UI is cut off

当我将我的应用程序加载到 phone 或使用模拟器时,我的 ListView 和 ImageButton 的底部被截断时遇到问题。我可以只使用边距或填充,但这不是特定于设备的吗?无论屏幕尺寸如何,我都希望我的应用程序看起来像我想要的那样。这是我的代码:

Class:

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 预览如下所示:

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

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

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

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

我认为 CoordinatorLayout 只适用于 NestedScrollingChild 的 children。

我最近也遇到过这种情况。我建议也发布您的 styles.xml

我的罪魁祸首是这行代码或类似的代码:

<item name="android:windowTranslucentStatus">true</item>

这是我针对类似问题发布的问题:

Android Design Library 中,好吧,FloatingActionButton 应该在 CoordinatorLayout 中,而不是在任何其他视图中,例如:上面选项卡之一中的片段视图。因此,请尝试将您的 FloatingActionButton 添加到您的 main_layout,然后仅与 activity 通信至 show/hide FAB 并进行必要的点击。

将下面的代码放在onCreateView 中,我认为它会起作用。

View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);

将布局更改为 RelativeLayout 对我有用。然后在 viewPager 标签中添加这一行:

android:layout_below="@id/appbar"

瞧瞧..!!