qq_33066259 2018-04-24 04:04 采纳率: 100%
浏览 1091
已采纳

Android 线性布局ScrollView嵌套以及TextView设置background的小问题

ScrollView 部分布局
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/part_line"
android:layout_above="@id/pay_go"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="5dp"
        android:orientation="vertical"
        android:id="@+id/pay_main_info">
        <include layout="@layout/add_address"
            android:id="@+id/address_desc"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            />
        <include layout="@layout/pay_other_info"
            android:id="@+id/pay_other_info"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="5px"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="桃李园"
            android:textSize="25dp"
            android:layout_marginLeft="20dp"
            android:textColor="@color/black"
            android:id="@+id/pay_order_form_canteen_name"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="5px"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"

            />
        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:id="@+id/pay_order_form_foodlist">

            </ListView>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="添加备注"
            android:drawableRight="@mipmap/more"
            android:textSize="20dp"
            android:layout_marginLeft="20dp"
            android:textColor="@color/black"
            android:layout_marginRight="20dp"
            android:clickable="true"
            android:id="@+id/pay_order_form_add_remakes"/>



    </LinearLayout>
</ScrollView>


    布局效果图:
    ![图片说明](https://img-ask.csdn.net/upload/201804/24/1524541534_568444.png)

    实际效果图:
    ![图片说明](https://img-ask.csdn.net/upload/201804/24/1524541656_487597.png)

    期望效果:
      实现类似于饿了么的那种界面,把listview的界面填充出去,如图下:

![图片说明](https://img-ask.csdn.net/upload/201804/24/1524542251_73813.png)

        TextView的设置background的bug:

        TextView代码:
        <TextView
        android:id="@+id/add_address_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:background="@drawable/text_bg"
        android:gravity="center"
        android:paddingBottom="20dip"
        android:paddingLeft="15dip"
        android:paddingRight="15dip"
        android:paddingTop="20dip"
        android:text="+添加收货地址"
        android:textColor="@color/theme_blue_two"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
       />
                效果:
                ![图片说明](https://img-ask.csdn.net/upload/201804/24/1524542466_880111.png)
                background的效果并没有运用上去,附其他可用的TextView的效果,如图下:
                ![图片说明](https://img-ask.csdn.net/upload/201804/24/1524542599_493676.png)
  • 写回答

3条回答 默认 最新

  • nades 2018-04-24 08:01
    关注

    你这属于滑动空间嵌套滑动空间问题 需要重写listview 要不然就会出现显示不全的问题 还有滑动冲突问题
    自定义一个listview 继承 listview 重写onmeasure 方法 重新测量高度

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int measureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, measureSpec);
    
    }
    

    很简单 重写这一个方法就可以 然后再xml 文件中引用就好了 你试一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • devmiao 2018-04-24 04:28
    关注

    ScrollView滚动视图是指当拥有很多内容、屏幕显示不完时、需要通过滚动跳来显示的视图、Scrollview的一般用法如下、以下代码在Scrollview里面放了一个RelativeLayout、并且是设置为android:layout_height="match_parent"填充全屏的,但是测试以后不起作用。布局如下:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/common_background" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/bottom_bg" />
    
    </RelativeLayout>
    


    查看android API 获知,
    其中白色部分是scrollView,灰色部分是TextView,很明显,scrollview(白色)已经扩展到最大高度了,但是其内部的TextView(灰色)却没有扩展.可明明TextView的layout_height="fill_parent",为什么没占满呢?是因为TextView的上层LinearLayout为wrap_content的原因吗?

    但是换成fill_parent还是一样的(实际上Scrollview的第一层View的layout_weight在sdk中是建议为wrap_content的)。

    后来在stackoverflow上找到了原因: http://stackoverflow.com/questions/2599837/linearlayout-not-expanding-inside-a-scrollview

    要让ScrollView内部元素的 fill_parent 起作用必须设置android:fillViewport="true"

    1

    2

    3

    4

    5

    <ScrollView

    android:layout_width="match_parent"
    
    android:layout_height="match_parent"
    
    android:fillViewport="true"
    
    评论
  • nades 2018-04-24 08:02
    关注

    完整的listview
    public class MyListView extends ListView {

    public MyListView(Context context) {
        super(context);
    }
    
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    
        super.onMeasure(widthMeasureSpec, measureSpec);
    
    }
    

    }

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Qt 不小心删除了自带的类,该怎么办
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册
  • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
  • ¥15 Python爬虫程序
  • ¥15 crypto 这种的应该怎么找flag?
  • ¥15 代码已写好,求帮我指出错误,有偿!