android ListView实现圆角实例教程二

57 阅读2分钟

www.eoeandroid.com/thread-1797…

对于简短的listview,加上如上面这些比较死的数据,我们完全没有必要用listview来实现,TableLout完全够了.主要是我们怎么实现带有弧度的边罢了,不过这也完全得力于shape的功劳,至于点击效果效果嘛,我们有selector,下面介绍下实现:

配置文件部分代码:

<ScrollView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scrollbars="none" >

<LinearLayout

android:id="@+id/ll_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:gravity="center"

android:orientation="vertical"

android:paddingLeft="10dp"

android:paddingRight="10dp" >

很简单,我们主要用这个Linerlayout来呈放我们的Table. 

实现代码:

package com.jj.corner;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import android.R.color;

import android.app.Activity;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.view.Window;

import android.widget.ArrayAdapter;

import android.widget.LinearLayout;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.RelativeLayout;

import android.widget.SimpleAdapter;

import android.widget.TableLayout;

import android.widget.TableRow;

import android.widget.TextView;

public class MainActivity2 extends Activity {

private LinearLayout ll_main;

private TableLayout tableLayout;

private LinearLayout.LayoutParams layoutParams;

private static final String MSG_0[] = { "jjhappyforever" };

private static final String MSG_1[] = { "天气动画", "通知栏天气" };

private static final String MSG_2[] = { "桌面插件", "绑定微博", "天气分享", "通知与提示",

"定时播报" };

private static final String MSG_3[] = { "检查新版本", "发送建议", "帮助", "关于" };

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.main2);

ll_main = (LinearLayout) findViewById(R.id.ll_main);

showTable();

}

/***

* 显示table

*/

public void showTable() {

layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT);

layoutParams.bottomMargin = 30;

layoutParams.topMargin = 10;

ll_main.addView(getTable(MSG_0), layoutParams);

ll_main.addView(getTable(MSG_1), layoutParams);

ll_main.addView(getTable(MSG_2), layoutParams);

ll_main.addView(getTable(MSG_3), layoutParams);

}

/***

* 获取Table

*

* @param array

* @return

*/

public TableLayout getTable(String[] array) {

tableLayout = new TableLayout(this);

tableLayout.setLayoutParams(layoutParams);

tableLayout.setStretchAllColumns(true);

for (int i = 0; i < array.length; i++) {

TableRow tableRow = new TableRow(this);

View view = getView(array[i], i, array.length);

tableRow.addView(view);

tableLayout.addView(tableRow);

}

return tableLayout;

}

/****

*

* @param msg

* 显示信息

* @param current_Id

* 当前个数

* @param totle_Num

* 总个数

* @return

*/

public View getView(String msg, int current_Id, int totle_Num) {

LinearLayout linearLayout = new LinearLayout(this);

LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

layoutParams1.height = 1;

linearLayout.setOrientation(1);

// 创建分割线

View line = new View(this);

line.setLayoutParams(layoutParams1);

line.setBackgroundColor(getResources().getColor(R.color.black));

View view = LayoutInflater.from(MainActivity2.this).inflate(

R.layout.item, null);

view.setBackgroundDrawable(new BitmapDrawable());

view.setFocusable(true);

view.setClickable(true);

TextView textView = (TextView) view.findViewById(R.id.tv_list_item);

textView.setText(msg);

textView.setTextSize(20);

// 只有一项

if (totle_Num == 1) {

view.setBackgroundResource(R.drawable.default_selector);

return view;

}

// 第一项

else if (current_Id == 0) {

view.setBackgroundResource(R.drawable.list_top_selector);

}

// 最后一项

else if (current_Id == totle_Num - 1) {

view.setBackgroundResource(R.drawable.list_bottom_selector);

line.setVisibility(View.GONE);

} else

view.setBackgroundResource(R.drawable.list_center_selector);

linearLayout.addView(view);

linearLayout.addView(line);

return linearLayout;

}

}

主要是getView有些复杂,不过都很好理解,相信大家都可以明白,只是没有动手做而已,小弟我只是想用这个练练手. 

不过不要忘记我们的背景实现

这个是只有一项的背景,

default_selector.xml

<item android:state_pressed="true">

### 架构师筑基包括哪些内容

我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容,详细完整版的【架构师筑基必备技能】文档领取方式:点赞+关注,然后私信关键词 【666】即可获得免费领取方式!或者 可以查看我的【Github

注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!

这份资料就包含了所有Android初级架构师所需的所有知识!需要的可以在我的GIthub里面去查看!