1. 程式人生 > >Android studio實現頁面渲染

Android studio實現頁面渲染

設置 weight contex count inf 渲染 launcher void mil

item.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:src="@mipmap/ic_launcher"
android:layout_width="60dp"
android:layout_height="60dp"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/iv_image"
android:text="Title"
android:gravity="center"
android:textSize="20sp"/>

<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height=
"wrap_content"
android:layout_toEndOf="@id/iv_image"
android:layout_below="@id/tv_title"
android:text="Content"
android:textSize="20sp"/>
</RelativeLayout>
activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools
="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fengxinzi.helloworld.MainActivity">

<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
對象
package com.example.fengxinzi.helloworld;

/**
* Created by fengxinzi on 2018/11/19.
*/

public class ItemBean {

public int itemImageResId;//圖像資源ID
public String itemTitle;//標題
public String itemContent;//內容

public ItemBean(int itemImageResId, String itemTitle, String itemContent) {
this.itemImageResId = itemImageResId;
this.itemTitle = itemTitle;
this.itemContent = itemContent;
}
}

適配器
package com.example.fengxinzi.helloworld;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
* Created by fengxinzi on 2018/11/19.
*/

public class MyAdapter extends BaseAdapter {

private List<ItemBean> mList;//數據源
private LayoutInflater mInflater;//布局裝載器對象

public MyAdapter(Context context, List<ItemBean> list) {
mList = list;
mInflater = LayoutInflater.from(context);
}


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

@Override
//指定的索引對應的數據項
public Object getItem(int position) {
return mList.get(position);
}

@Override
//指定的索引對應的數據項ID
public long getItemId(int position) {
return position;
}

@Override
//返回每一項的顯示內容
public View getView(int position, View convertView, ViewGroup parent) {
//將布局文件轉化為View對象
View view = mInflater.inflate(R.layout.item,null);

/**
* 找到item布局文件中對應的控件
*/
ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);
TextView titleTextView = (TextView) view.findViewById(R.id.tv_title);
TextView contentTextView = (TextView) view.findViewById(R.id.tv_content);

//獲取相應索引的ItemBean對象
ItemBean bean = mList.get(position);

/**
* 設置控件的對應屬性值
*/
imageView.setImageResource(bean.itemImageResId);
titleTextView.setText(bean.itemTitle);
contentTextView.setText(bean.itemContent);

return view;
}
}

mainActivity
package com.example.fengxinzi.helloworld;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

ListView mListView ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<ItemBean> itemBeanList = new ArrayList<>();
for (int i = 0;i < 20; i ++){
itemBeanList.add(new ItemBean(R.mipmap.ic_launcher, "標題" + i, "內容" + i));
}
mListView = (ListView) findViewById(R.id.lv_main);
//設置ListView的數據適配器
mListView.setAdapter(new MyAdapter(this,itemBeanList));




}

}

Android studio實現頁面渲染