【问题标题】:Android LinearLayout not displaying correctly if number of items is lesser then screen height如果项目数小于屏幕高度,Android LinearLayout 无法正确显示
【发布时间】:2015-04-18 09:14:31
【问题描述】:

我有一个片段,它从 rest api 获取项目并在滚动视图中的 LinearLayout 中显示项目标题。

以下是我的观点和代码。

fragment_cards_view.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:fillViewport="true">

        <LinearLayout
            android:id="@+id/linear_card_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="@color/wallet_holo_blue_light"/>

</ScrollView>

list_item.xml

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/white"
                    android:layout_marginBottom="10dp">
    <TextView
        android:id="@+id/list_titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        style="@style/item_header"/>

   </RelativeLayout>
</RelativeLayout>

TitleFragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i(TAG, "In onCreateView");

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_cards_view, container, false);

        mLinearLayout = (LinearLayout)v.findViewById(R.id.linear_card_view);

        return v;
    }

private void createUpcomingView() {  
            int counter = 0;
            for (User user : mUsers) {
                LayoutInflater inflater = null;
                inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View mLinearView = inflater.inflate(R.layout.list_item, null);
                mLinearView.setId(user.getId());

                TextView titleTextView = (TextView)mLinearView.findViewById(R.id.list_titleTextView);
                titleTextView.setText("title " + counter);
                mLinearLayout.addView(mLinearView);
                counter++;
            }
        }

我遇到的问题是,如果item的数量小于屏幕高度,就会显示不正确。

此屏幕截图有 4 个项目,但仅显示第一个项目,并且边距未正确对齐。

此屏幕截图包含许多项目,并且边距正确对齐。

这个截图是第一个解决方案:

【问题讨论】:

    标签: android android-linearlayout android-scrollview


    【解决方案1】:

    从 list_item.xml 中删除以下布局,然后它将正常工作。

    <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="@color/white"
                                android:layout_marginBottom="10dp">
    
    </RelativeLayout>
    

    【讨论】:

    • 嗨,删除子 relativeLayout 后,marginRight 问题仍然存在
    • 你想要的边距在哪里?
    【解决方案2】:

    试着写

    inflater.inflate(R.layout.list_item, mLinearLayout, false);
    

    而不是

    inflater.inflate(R.layout.list_item, null);
    

    另外,在 list_item.xml 中使用 wrap_content 高度

    【讨论】:

    • 您好,感谢您的回复。我添加了 mLinearLayout 来充气并将 wrap_contents 设置为 list_item.xml 中的两个 relativeLayout。我已经上传了另一个显示结果的屏幕截图。显然,边距已关闭..
    【解决方案3】:

    我根据@clemp6r 和@Jain Nidhi 的回答找到了解决方案。

    fragment_cards_view.xml

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/scrollView"
                android:fillViewport="true">
    
            <LinearLayout
                android:id="@+id/linear_card_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="@color/wallet_holo_blue_light"/>
    
    </ScrollView>
    

    list_item.xml,添加左右边距,移除嵌套RelativeLayout,设置所有layout_height为wrap_content

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
    
        <TextView
            android:id="@+id/list_titleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            style="@style/item_header"/>
    
    </RelativeLayout>
    

    TitleFragment.java,修改了inflate的第2个和第3个参数。

    private void createUpcomingView(..) {
    View mLinearView = inflater.inflate(R.layout.list_item, mLinearLayout, false);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 2019-12-23
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多