android ScrollView和Wrap_Content不起作用

bsxbgnwa  于 2022-11-20  发布在  Android
关注(0)|答案(2)|浏览(170)

我的xml中有一个问题,当我的RelativeLayout为wrap_content时,ScrollView不工作,但如果我的屏幕大于固定高度,则工作正常,但如果我的固定高度大于屏幕,则工作正常,但我需要为wrap_content,因为我有一个根据客户端需要逐渐填充的列表视图,遵循xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
android:orientation="horizontal" 
android:baselineAligned="false">

<LinearLayout 
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#fff">
    </LinearLayout>

        <ScrollView 
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="45"
            android:fillViewport="true"
            android:scrollbars="vertical">

            <RelativeLayout 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"
                android:padding="2dp"
                android:background="@drawable/borda"
                android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="top|center_horizontal"
                        android:id="@+id/lb_cabec"
                        android:text="@string/str_cabec" />

                    <TextView
                        android:id="@+id/lb_pessoa_status"
                        android:paddingTop="4dp"
                        android:paddingBottom="15dp"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="bottom"
                        android:textStyle="bold"
                        android:text="@string/pessoas_lb_mesa_cartao" 
                        android:layout_below="@+id/lb_cabec"/>

                     <RelativeLayout 
                        android:layout_height="wrap_content" 
                        android:layout_width="fill_parent"
                        android:padding="2dp"
                        android:background="@drawable/borda"
                        android:id="@+id/relative_prod"
                        android:layout_below="@+id/lb_pessoa_status">

                        <TextView
                            android:layout_width="fill_parent"
                            android:id="@+id/lb_prodMistAdd_cabec_prod"
                            android:layout_height="wrap_content"
                            android:background="#000"
                            android:textColor="#fff"
                            android:gravity="center_horizontal"
                            android:text="@string/prodMistAdd_str_prod" />

                        <ListView
                            android:id="@+id/mListProdMistAdd_prod"
                            android:layout_width="fill_parent"
                            android:layout_marginBottom="10dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/lb_prodMistAdd_cabec_prod" />

                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="35dp"
                            android:layout_below="@+id/mListProdMistAdd_prod"
                            android:layout_centerHorizontal="true"
                            android:gravity="center_horizontal"
                            android:id="@+id/bt_prodMistAdd_addProd"
                            android:text="@string/prodMistAdd_bt_prod" />

                    </RelativeLayout>

                     <LinearLayout 
                        android:layout_height="10dp" 
                        android:layout_width="fill_parent"
                        android:id="@+id/lb_espaco_1"
                        android:layout_below="@+id/relative_prod"
                        android:orientation="vertical">

                     </LinearLayout>

                     <RelativeLayout
                        android:layout_height="wrap_content" 
                        android:layout_width="fill_parent"
                        android:padding="2dp"
                        android:background="@drawable/borda"
                        android:id="@+id/relative_quant"
                        android:layout_below="@+id/lb_espaco_1">

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/lb_prodMistAdd_cabec_quant"
                            android:background="#000"
                            android:textColor="#fff"
                            android:gravity="center_horizontal"
                            android:text="@string/prodMistAdd_str_quant" />

                        <LinearLayout 
                            android:layout_height="5dp" 
                            android:id="@+id/tab_espaco_2"
                            android:layout_below="@+id/lb_prodMistAdd_cabec_quant"
                            android:layout_width="fill_parent"
                            android:orientation="vertical"
                            android:background="#fff">
                        </LinearLayout>

                        <LinearLayout 
                            android:layout_height="wrap_content" 
                            android:layout_width="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:layout_below="@+id/tab_espaco_2"
                            android:orientation="horizontal">

                            <EditText
                                android:id="@+id/tb_prodMistAdd_quantidade"
                                android:layout_width="60dp"
                                android:layout_height="wrap_content"
                                android:editable="false"
                                android:gravity="center_vertical|right"
                                android:ems="10"
                                android:inputType="textPersonName" />

                            <Button
                                android:layout_width="40dp"
                                android:layout_height="35dp"
                                android:gravity="center_horizontal"
                                android:id="@+id/bt_prodMistAdd_mais"
                                android:text="@string/prodMistAdd_bt_mais" />

                            <Button
                                android:layout_width="40dp"
                                android:layout_height="35dp"
                                android:gravity="center_horizontal"
                                android:id="@+id/bt_prodMistAdd_menos"
                                android:text="@string/prodMistAdd_bt_menos" />

                        </LinearLayout>

                     </RelativeLayout>

                     <LinearLayout 
                        android:layout_height="10dp" 
                        android:layout_width="fill_parent"
                        android:id="@+id/lb_espaco_2"
                        android:orientation="vertical"
                        android:layout_below="@+id/relative_quant">

                     </LinearLayout>

                     <RelativeLayout 
                        android:layout_height="wrap_content" 
                        android:layout_width="fill_parent"
                        android:padding="2dp"
                        android:background="@drawable/borda"
                        android:id="@+id/relative_obs"
                        android:layout_below="@+id/lb_espaco_2">

                        <TextView
                            android:layout_width="fill_parent"
                            android:id="@+id/lb_prodMistAdd_cabec_obs"
                            android:layout_height="wrap_content"
                            android:background="#000"
                            android:textColor="#fff"
                            android:gravity="center_horizontal"
                            android:text="@string/prodMistAdd_str_obs" />

                        <LinearLayout 
                            android:layout_height="5dp" 
                            android:id="@+id/tab_espaco_3"
                            android:layout_below="@+id/lb_prodMistAdd_cabec_obs"
                            android:layout_width="fill_parent"
                            android:orientation="vertical"
                            android:background="#fff">
                        </LinearLayout>

                        <ListView
                            android:id="@+id/mListProdMistAdd_obs"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:scrollbars="none"
                            android:layout_below="@+id/tab_espaco_3" />

                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="35dp"
                            android:layout_below="@+id/mListProdMistAdd_obs"
                            android:layout_centerHorizontal="true"
                            android:gravity="center_horizontal"
                            android:id="@+id/bt_log_voltar"
                            android:text="@string/prodMistAdd_bt_obs" />

                </RelativeLayout>       
            </RelativeLayout>       
        </ScrollView>

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1">

    </LinearLayout>

  </LinearLayout>

我的列表视图配置为不滚动,通过代码

q5iwbnjs

q5iwbnjs1#

scrollview内部的listview是否工作正常?
我认为真实的的问题是这个。
Scrollview中的listview不是一个好的做法。Listview将尝试实现自己的滚动。但是父Scrollview将占用所有的手势。因此Listview不能是Scrollview的子。
如果你还想这样做的话
试试这个

ListView lv = (ListView)findViewById(R.id.myListView);  // your listview inside scrollview
lv.setOnTouchListener(new ListView.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            // Disallow ScrollView to intercept touch events.
            v.getParent().requestDisallowInterceptTouchEvent(true);
            break;

        case MotionEvent.ACTION_UP:
            // Allow ScrollView to intercept touch events.
            v.getParent().requestDisallowInterceptTouchEvent(false);
            break;
        }

        // Handle ListView touch events.
        v.onTouchEvent(event);
        return true;
    }
});
baubqpgj

baubqpgj2#

尝试将ScrollView layout_widthlayout_height定义为match_parent,而不使用layout_weight
顺便说一句,你不应该混合ScrollViewListView,因为他们都有自己的卷轴.我建议这样拆分:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        (...)
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

相关问题