Code layout for Android programming (III)

Source: Internet
Author: User

The first two articles introduce the commonly used layout component writing methods. This time, let's take a look at

1. listview

Like most components, listview is generally difficult here, that is, when you customize layout styles.

// Create the linearlayout object linearlayout mrelativelayout = new linearlayout (this); mrelativelayout. setlayoutparams (New linearlayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); // create the listview object listview mlistview = new listview (this); mlistview. setadapter (New listadapter (this, mvector); // Add the mrelativelayout component. addview (mlistview, new layoutparams (layoutparams. fill_parent, layoutparams. fill_parent ));

Listadapter is the custom class that inherits the baseadapter.

Mvector is our custom data, which contains two names: James and Xiaogang.

Next, let's take a look at how the key listadapter customizes the layout.

public class ListAdapter extends BaseAdapter {private Vector<String> mVector;private Context mContext;public ListAdapter(Context mContext, Vector<String> mVector) {this.mContext = mContext;this.mVector = mVector;}public Object getItem(int position) {return null;}public long getItemId(int position) {return 0;}public int getCount() {return mVector.size();}class ViewHolder {ImageView head;TextView name;}public View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = null;RelativeLayout rl = null;if (convertView == null) {rl = new RelativeLayout(mContext);rl.setBackgroundDrawable(bitmapDrawable_listitembg);// headImageView head = new ImageView(mContext);head.setId(1);RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(64, 64);mLayoutParams.topMargin = 15;mLayoutParams.leftMargin = 10;mLayoutParams.bottomMargin = 15;mLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);rl.addView(head, mLayoutParams);// nicknameTextView mTextView = new TextView(mContext);mTextView.setTextSize(20);mTextView.setTextColor(0xff000000);mTextView.setId(2);mLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);mLayoutParams.topMargin = 10;mLayoutParams.leftMargin = 10;mLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);rl.addView(mTextView, mLayoutParams);holder = new ViewHolder();holder.head = head;holder.name = mTextView;rl.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();rl = (RelativeLayout) convertView;}holder.head.setImageDrawable(bitmapDrawable_headimage);holder.name.setText(mVector.elementAt(position));return rl;}}

If you have read the relative layout mentioned above, it is not difficult to understand the custom layout.
A portrait imageview and a textview are defined. Make the Avatar a distance from the upper left to the lower left, display the name on the right of the Avatar, and make a certain distance from the right of the Avatar.

Settings of the layout background image: RL. setbackgrounddrawable (bitmapdrawable_listitembg); the set image is read from the asset, and the Avatar is also.

The final display effect is attached:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.