安卓实习第九天

- Scrollview嵌套ListView

/**
* 动态设置ListView的高度
* @param listView
*/
public static void setListViewHeightBasedOnChildren(ListView listView) { 
    if(listView == null) return;

    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) { 
        // pre-condition 
        return; 
    } 

    int totalHeight = 0; 
    for (int i = 0; i < listAdapter.getCount(); i++) { 
        View listItem = listAdapter.getView(i, null, listView); 
        listItem.measure(0, 0); 
        totalHeight += listItem.getMeasuredHeight(); 
    } 

    ViewGroup.LayoutParams params = listView.getLayoutParams(); 
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    listView.setLayoutParams(params); 
}

上面这个方法就是设定ListView的高度了,在为ListView设置了Adapter之后使用,就可以解决问题了。

但是这个方法有个两个细节需要注意:
一是Adapter中getView方法返回的View的必须由LinearLayout组成,因为只有LinearLayout才有measure()方法,如果使用其他的布局如RelativeLayout,在调用listItem.measure(0, 0);时就会抛异常,因为除LinearLayout外的其他布局的这个方法就是直接抛异常的。
二是需要手动把ScrollView滚动至最顶端,因为使用这个方法的话,默认在ScrollView顶端的项是ListView,具体原因不了解。

优点是不用对使用的控件做任何修改,只需要使用一个现成的方法就好了,而最大的限制是ListView的item只能由LinearLayout这一个布局组成,对于一些复杂的布局就不适用了。如果你的工程急需解决这个问题,而且满足方法的使用条件,即ListView的item布局简单,完全有LinearLayout组成,你就只需要把setListViewHeightBasedOnChildren方法拿过去就行了。

- 实现RadioButton的drawableTop图片大小修改

在res/values/styles中加入

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

    <declare-styleable name="MyRadioButton">  

        <attr name="drawableSize" format="dimension"/>  
        <attr name="drawableTop" format="reference"/>  
        <attr name="drawableLeft" format="reference"/>  
        <attr name="drawableRight" format="reference"/>  
        <attr name="drawableBottom" format="reference"/>  

    </declare-styleable>  

</resources>  

新建类

package com.example.test;  

import android.R.integer;  
import android.R.raw;  
import android.content.Context;  
import android.content.res.Resources;  
import android.content.res.TypedArray;  
import android.graphics.drawable.Drawable;  
import android.util.AttributeSet;  
import android.util.Log;  
import android.widget.RadioButton;  

public class MyRadioButton extends RadioButton {  

    private int mDrawableSize;// xml文件中设置的大小  

    public MyRadioButton(Context context) {  
        this(context, null, 0);  
    }  

    public MyRadioButton(Context context, AttributeSet attrs) {  
        this(context, attrs, 0);  
    }  

    public MyRadioButton(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        // TODO Auto-generated constructor stub  
        Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;  
        TypedArray a = context.obtainStyledAttributes(attrs,  
                R.styleable.MyRadioButton);  

        int n = a.getIndexCount();  
        for (int i = 0; i < n; i++) {  
            int attr = a.getIndex(i);  
            Log.i("MyRadioButton", "attr:" + attr);  
            switch (attr) {  
            case R.styleable.MyRadioButton_drawableSize:  
                mDrawableSize = a.getDimensionPixelSize(R.styleable.MyRadioButton_drawableSize, 50);  
                Log.i("MyRadioButton", "mDrawableSize:" + mDrawableSize);  
                break;  
            case R.styleable.MyRadioButton_drawableTop:  
                drawableTop = a.getDrawable(attr);  
                break;  
            case R.styleable.MyRadioButton_drawableBottom:  
                drawableRight = a.getDrawable(attr);  
                break;  
            case R.styleable.MyRadioButton_drawableRight:  
                drawableBottom = a.getDrawable(attr);  
                break;  
            case R.styleable.MyRadioButton_drawableLeft:  
                drawableLeft = a.getDrawable(attr);  
                break;  
            default :  
                    break;  
            }  
        }  
        a.recycle();  

        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);  

    }  

    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left,  
            Drawable top, Drawable right, Drawable bottom) {  

        if (left != null) {  
            left.setBounds(0, 0, mDrawableSize, mDrawableSize);  
        }  
        if (right != null) {  
            right.setBounds(0, 0, mDrawableSize, mDrawableSize);  
        }  
        if (top != null) {  
            top.setBounds(0, 0, mDrawableSize, mDrawableSize);  
        }  
        if (bottom != null) {  
            bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);  
        }  
        setCompoundDrawables(left, top, right, bottom);  
    }  

}  

在xml文件中布局设置为xmlns:myradio="http://schemas.android.com/apk/res/com.example.test"
如下为一个RadioButton

<com.example.test.MyRadioButton  
    android:id="@+id/tab_a"  
    android:layout_width="0dp"  
    android:layout_height="wrap_content"  
    android:layout_weight="1"  
    android:layout_marginLeft="10dp"  
    android:layout_marginRight="10dp"  
    android:background="@color/white"  
    android:button="@null"  
    android:clickable="true"  

    myradio:drawableTop="@drawable/shouye_radiobutton"  
    myradio:drawableSize="20dp"  


    android:gravity="center"  
    android:text="首页"  
    android:textSize="13sp" />  

详见http://blog.csdn.net/gaoshouxiaodi/article/details/44565781

- 给手机装上sqlite3

1.打开模拟器,进入/system/xbin看是否有sqlite3
2.将其拉到mac上
adb pull /system/xbin/sqlite3 /Users/zhangkaiyue
3.使用Android Studio的DDMS将sqlite3拉到/system/xbin中
4.修改权限
chmod 4755 /system/xbin/sqlite3
5.执行
adb shell
sqlite3

PS:sqlite3下载地址:http://dev.evelio.info/sqlite3-4.1 for Jelly Bean and later here

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值