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

在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就行了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值