您的位置:首页 > 移动开发 > Android开发

Android 动态设置列表样式,不用ListView或者RecyclerView

2017-08-26 14:39 585 查看
在Android中会有遇到这种情况,在动态添加列表型数据时,在不用listview,RecyclerView等情况下用列表形式展示数据,那么久用到绝对布局中LinerLayout进行动态设置。先了解一下LinerLayout有哪些属性,我们用到哪几种。我们就了解常用的吧

android:orientation=”vertical”//设置重心,horizontal横向显示,在我们用到的就是vertical,纵向显示的

<LinearLayout
android:id="@+id/linerAdd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />


先看看Activity中的代码

public class LinearActivity extends Activity {
private LinearLayout linerAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear);
linerAdd = (LinearLayout) findViewById(R.id.linerAdd);
init();
}
public void init() {
//先添加5条数据
linerAdd.removeAllViews();//移除
for (int i = 0; i < 5; i++) {
View view = LayoutInflater.from(this).inflate(R.layout.item_linear, null);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
tv_content.setText("我是第" + i + "条");
linerAdd.addView(view);
}
}
}


我们先看看效果



这个没有线条,那么我们加一条 横线



现在有了,这个是怎么做到的呢,很简单,在子布局中增加View设置背景颜色

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:background="#e9e9e9" />

<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="Large Text"
android:textColor="#000"
android:textSize="16sp" />

<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:background="#e9e9e9" />

</LinearLayout>


说了这么多怎么跟标题符合呢,嘿嘿,我们再看一下另一个效果



很明显,箭头部分的横线要粗一些,因为这是主布局中的View和子布局中View重合了,那么解决这个就是去掉主布局中View就行了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android
相关文章推荐