LinearLayout遇到的那些坑

LinearLayout是我们经常使用的ViewGroup相信大家对这个空间应该特别熟悉吧,下面我就说一下我工作的时候遇到的一些小坑,希望可以让您在遇到类似的问题可以尽快的避免

先给大家看一个效果图

实现的方式就是 在主布局中写个LinearLayout

<LinearLayout
    android:id="@+id/contact_main_list"
    style="@style/Layout.Horizontal"
    android:orientation="vertical"></LinearLayout>

自定义一个item

<?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="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/view_left_margin_size"
        android:layout_marginRight="@dimen/view_left_margin_size"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:minHeight="@dimen/list_item_height">
        <TextView
            android:id="@+id/left_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="#999999"
            android:text="固话"/>
        <TextView
            android:id="@+id/right_number"
            android:layout_marginLeft="32dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="#333333"
            android:text="18656666742"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/detail_linear"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@color/text_gray_g_color" />
</LinearLayout>
通过遍历一个集合 new item() 并设置内容  用LinearLayout.addView(View)添加就会成为上面红色的效果  我们可以看到每一个item的布局我们都设置的match_parent,但是怎么还是成为了自适应

解决方案: 将addView(View) 换成addView(View,params)

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
addView(parent, layoutParams);



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章