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

时间:2015-11-03 00:21:41

标签: android android-layout android-listview android-imagebutton

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

班级:

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

Android Studio Preview

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

Emulated UI display

5 个答案:

答案 0 :(得分:1)

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

或如果您正在使用5.0以上,则可以使用以下代码

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

我认为CoordinatorLayout仅适用于NestedScrollingChild的孩子。

答案 1 :(得分:0)

我最近才发生这种情况。我建议您也发布styles.xml

对我来说,罪魁祸首就是这行代码或类似代码:

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

这是我发布的类似问题的问题: Layout is under StatusBar and Soft Keys

答案 2 :(得分:0)

Android Design Library中,FloatingActionButton应该在CoordinatorLayout内,而不是在任何其他视图中:在上面某个标签中的片段视图中。因此,请尝试将FloatingActionButton添加到main_layout,然后与活动进行通信,以显示/隐藏FAB并进行必要的点击。

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

android:layout_below="@id/appbar"

Voilla .. !!