在android中布局很重要,下面总结下布局的三种形式
①.在Activity的onCreate()方法中进行添加比如: setContentView(R.layout.activity_main);或者
setContentView(this.getResources().getIdentifier("homeleftmenu_layout", "layout", this.getPackageName()) );
②此种情况多用在适配器中进行加载,如下:
一种方法是用View.inflate()方法;
代码说明:前提是自定义了一个参数: private Context context;
@override代码是自定义一个适配器CalendarAdapter extends BaseAdapter中的getView()方法中来添加一个自定义的布局
calendar_cell_layout.xml.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(context,R.layout.calendar_cell_layout,null);
holder = new ViewHolder();
holder.dayTextView = (TextView)convertView.findViewById(R.id.jkdayid);
holder.dayNLTextView = (TextView)convertView.findViewById(R.id.jkday_nlid);
holder.isdataImageView = (ImageView)convertView.findViewById(R.id.isdataid);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
public class ViewHolder {
public TextView dayTextView;
public TextView dayNLTextView;
public ImageView isdataImageView;
}
另外一种形式如下:用LayoutInflater.from(Context).inflate()方法。
代码说明:是一个自定义的 class DeviceListAdapter extends BaseAdapter中的override的getView()方法。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(MainActivity.this).inflate(
R.layout.list_bluetooth_item, null);
viewHolder = new ViewHolder();
viewHolder.tv_devName = (TextView) convertView
.findViewById(R.id.tv_blueth_name);
viewHolder.tv_devAddress = (TextView) convertView
.findViewById(R.id.tv_blueth_location);
viewHolder.tv_devStatus=(TextView)convertView.findViewById(R.id.tv_blueth_status);
convertView.setTag(viewHolder);
} else {
convertView.getTag();
}
}
}
class ViewHolder {
TextView tv_devName, tv_devAddress,tv_devStatus;
}
③写一个简单的方法来创建一个view对象:
private View initView() {
// TODO Auto-generated method stub
TextView textView = new TextView(this);
textView.setText("title");
textView.setTextSize(20);
textView.setTextColor(Color.RED);
textView.setGravity(Gravity.CENTER);
LinearLayout ll = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
ll.setBackgroundColor(Color.WHITE);
ll.addView(textView, params);
return ll;
}
④这种方法也是在适配器中进行创建的,关键代码部分如下:
说明:自定义一个适配器继承BaseAdapter,其中的context是要使用这个适配器的一个Activity, scene_layers_item是布局文件的名称,其中的iten_tv ,和item_cb是id,list是 List
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(context.getResources().getIdentifier("scene_layers_item", "layout",context.getApplication().getPackageName()), null);
holder.tv = (TextView)convertView.findViewById(context.getResources().getIdentifier("item_tv", "id",context.getApplication().getPackageName()));
holder.cb= (CheckBox)convertView.findViewById(context.getResources().getIdentifier("item_cb", "id",context.getApplication().getPackageName()));
holder.cb.setVisibility(View.VISIBLE);
convertView.setTag(holder);
}else
{
holder = (ViewHolder)convertView.getTag();
}
HashMap itemMap = list.get(position);
if(itemMap != null)
{
String layname = (String)itemMap.get("nameshow");
if(layname != null)
{
holder.tv.setText(layname);
}
holder.cb.setChecked((Boolean)itemMap.get("isshow"));
}
return convertView;
}
public class ViewHolder {
public TextView tv = null;
public CheckBox cb = null;
}
⑤其实在对各种布局都熟悉的情况下,采用自定义布局,对android系统提供的view进行继承,重写其中的方法,达到想要的效果后,然后在需要的地方进行引用。
以上均来自个人总结,如有不妥当之处,还烦请各位编程爱好者批评指正,共同进步!!!