Android ScrollView和Wrap_内容无效
我的xml中有一个问题,当我的RelativeLayout为wrap_内容时,ScrollView不起作用,但如果我的屏幕比固定高度大,则可以正常工作,但如果我的固定高度比屏幕大,则可以正常工作,但我需要是wrap_内容,因为我有一个listview,它会根据客户的需要逐渐填充,遵循xmlAndroid ScrollView和Wrap_内容无效,android,scrollview,android-relativelayout,Android,Scrollview,Android Relativelayout,我的xml中有一个问题,当我的RelativeLayout为wrap_内容时,ScrollView不起作用,但如果我的屏幕比固定高度大,则可以正常工作,但如果我的固定高度比屏幕大,则可以正常工作,但我需要是wrap_内容,因为我有一个listview,它会根据客户的需要逐渐填充,遵循xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com
<?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>
我的列表视图配置为不滚动,通过代码是否在
滚动视图内的列表视图工作
我认为真正的问题是这个
滚动视图
中的列表视图
不是一个好的做法。
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;
}
});
尝试将滚动视图layout\u width
和layout\u height
定义为match\u parent,而不使用layout\u weight
顺便说一句,你不应该把ScrollView
和ListView
混在一起,因为两者都有自己的滚动条。我建议将其拆分为:
<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>
(...)
您所说的“它不工作”是什么意思?当相对布局增加并通过屏幕大小时,滚动视图不会打开滚动。很抱歉没有具体说明,看看这个问题:还有,为什么你设置了android:layout\u width=“0dp”
,在ScrollView前后加了两个LinearLayout?为了创建边距,我是android的新手,我是这样发现的,你认为是这样吗?@RafiKamal你的说法非常有用,可能想作为回应,所以我选择了Sorry,我不理解你的问题,listview没有滚动,因为我在java类中从他那里删除了它,我将在问题中添加这些信息。这就是你所说的?好吧,我正在尝试,但是listview不会干扰滚动,似乎发生的是,相对布局不会超过屏幕的大尺寸,即使包装内容,但我需要一个边距,匹配父对象占据水平方向的所有空间,尽管在这种情况下不工作,但listview在保持较大时不会消失?在这种情况下,您有两个独立的内容:-scrollView中的所有内容都有自己的滚动(如果需要),listview中的所有内容都有-ListView只有在里面没有数据的情况下才会消失当然(包装内容)我不希望它有更多的滚动,希望它能作为一种gridview,一个简单的列表,它不需要滚动它,只需滚动页面。可能吗?或者我尝试其他方式?了解ScrollView有自己的滚动属性和listView其他属性。这就是为什么你不应该把它弄混的原因。