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);



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