安卓常用属性

安卓新人,自己平常开发当中常用的一些小方法,小技巧,可用来复制粘贴的代码块.后续会继续跟进.

1.透明的布局主题:android:theme="@android:style/Theme.Translucent.NoTitleBar"

2.异步线程的handler机制:
发起:
Message mg = Message.obtain();    
mg.obj = "a1";    
handler.sendMessage(mg);
接收:
private Handler handler = new Handler() {    
    @Override    
    public void handleMessage(Message msg) { 
        String m = (String) msg.obj;
        System.out.println("m"+m);
        if(m.equals("a1")){
            finish();//执行消息接收后的方法
        }
    }    
};

3.布局theme没有主题:@android:style/Theme.Light.NoTitleBar

4.禁止横屏:android:screenOrientation="portrait"

5.默认不选中edittext的光标:android:windowSoftInputMode="adjustPan|stateHidden"

6.listview与scrollview的冲突重组:
listview.setAdapter(adapter01);
setListViewHeightBasedOnChildren(listview);
在listview后面setadapter   加上setListViewHeightBasedOnChildren(listview);即可
//////////////////设置listview的高度
public void setListViewHeightBasedOnChildren(ListView listView) {   
    // 获取ListView对应的Adapter 
    ListAdapter listAdapter = listView.getAdapter();   
    if (listAdapter == null) {   
        return;   
    }   

    int totalHeight = 0;   
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   
    // listAdapter.getCount()返回数据项的数目 
    View listItem = listAdapter.getView(i, null, listView);   
    // 计算子项View 的宽高 
    listItem.measure(0, 0);    
    // 统计所有子项的总高度 
    totalHeight += listItem.getMeasuredHeight();    
    }   

    ViewGroup.LayoutParams params = listView.getLayoutParams();   
    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));   
    // listView.getDividerHeight()获取子项间分隔符占用的高度 
    // params.height最后得到整个ListView完整显示需要的高度 
    listView.setLayoutParams(params);   
}  

7.startactivityforresult启动activity的回调:
//启动回调
startActivityForResult(intent, 1001);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1001 && resultCode == Activity.RESULT_OK) {
        Bundle bundle = data.getExtras();
        System.out.println("str:"+bundle.get("str"));
    }
}
//返回带参
Intent intent=getIntent();
intent.putExtra("str", "abcd");
setResult(Activity.RESULT_OK,intent);
finish();

8.adapter 的继承
public class Fanli_adapter extends BaseAdapter{

    private Context context;
    private List<Map<String, String>>list= new ArrayList<Map<String,String>>();

    public Fanli_adapter(Context context, List<Map<String, String>> list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.item_activity_fg5_fan_li, null);
            holder=new ViewHolder();
            holder.tv1=(TextView) convertView.findViewById(R.id.item_fanli_img1);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }
        holder.tv1.setText((String)(list.get(position).get("1")));
        return convertView;
    }

    class ViewHolder{
        TextView tv1;
    }
}

9.xml内容/边线/弧角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffffff"  />  

    <stroke
        android:width="3dp"
        android:color="@null" />

    <corners 
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp"
        android:bottomLeftRadius="25dp"
        android:bottomRightRadius="25dp"
        />

</shape>

10.textview两行显示
android:maxLines="2"
android:ellipsize="end"

11.listview和gridview不选中的黄色背景
android:listSelector="@android:color/transparent"


12.加载的布局
//加载的布局
private View add_view(){
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.aitem, null);
    return view;
}

13.配合translate主题使用的activity的动画效果
overridePendingTransition(R.anim.ap2, R.anim.ap1);// 淡出淡入动画效果
anim.ap1:
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- fromAlpha:开始时透明度;toAlpha:结束时透明度;duration:动画持续时间 -->
    <alpha
        android:duration="100"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>
anim.ap2:
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="100"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>

等等:后续继续跟进.

你可能感兴趣的:(android,线程,Adapter,activity动画,ListView高度)