加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Android:ListView和重写后MyListView的简单使用2

(2023-07-01 20:27:35)
分类: androidios
转:https://blog.csdn.net/ssh159/article/details/55210899

3.activity_plan.xml

我们有一个头像photo1,一个标题title1, 一个隐藏的标题title 2(占位,把图标顶向右边),

一个向右的图标photo2!

 



   
   
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="8dp">
     
            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
                    android:id="@+id/photo1"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_margin="5dp"
            android:src="@drawable/plan1"/>
                    android:id="@+id/title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="训练须知"
            android:textSize="25dp"
            android:textColor="@color/cbrown"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"/>
     
                    android:id="@+id/title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
                    android:id="@+id/photo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/go_in"
            android:layout_gravity="center"/>
   
     
   


结果:



二、重写ListView 的例子!


有时候,我们的布局不是LinearLayout 那么简单,

由于页面比较长,我们使用了 ScrollView 可滑动布局!

但是ScrollView 布局中,ListView 只显示一行数据啊!

这个时候,我们就需要用到MyListView !




MyListView : 如图,好像一行都不显示了? #滑稽




别担心!虽然在studio 中不显示,但是在手机中,虚拟机中还是正常的!


我们重写一下ListView !

1、MyListView.java  (新建一个activity ,但是没有xml )


    package com.open_open.android_plantest;
     
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ListView;
     
   
     
    public class MyListView extends ListView {
     
        public MyListView(Context context) {
            // TODO Auto-generated method stub
            super(context);
        }
     
        public MyListView(Context context, AttributeSet attrs) {
            // TODO Auto-generated method stub
            super(context, attrs);
        }
     
        public MyListView(Context context, AttributeSet attrs, int defStyle) {
            // TODO Auto-generated method stub
            super(context, attrs, defStyle);
        }
     
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        }
    }


2、ListView 改为 MyListView




改完后,看看自己的项目是不是照样可以用呢?


PS : ListView 也自带可 滑动的效果!
————————————————
版权声明:本文为CSDN博主「凉城古梦喵扑街」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ssh159/article/details/55210899
 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有