【Android多屏適配】動態改變Listview item高度

來源:互聯網
上載者:User

在ListView的Adapter中去直接擷取傳入View的LayoutParams是會報null 指標異常的,唯一的方法是在xml中嵌套布局一層LinearLayout

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:background="@drawable/subject_item_bg"    android:orientation="vertical" >    <!-- 這層需要嵌套 -->    <LinearLayout        android:id="@+id/subject_ll"        android:layout_width="fill_parent"        android:layout_height="120dp"        android:orientation="vertical" >        <ImageView            android:id="@+id/subject_ico"            android:layout_width="fill_parent"            android:layout_height="0dp"            android:layout_weight="17"            android:layout_margin="5dp"            android:scaleType="fitXY"            android:src="@drawable/woji" />        <TextView            android:id="@+id/subject_title"            android:layout_width="fill_parent"            android:layout_height="0dp"            android:layout_weight="5"            android:layout_marginLeft="10dp"            android:singleLine="true"            android:text="曾經啊減肥的撒飛機 合集合集合集肥的撒飛機 合肥的撒飛機 合"            android:textColor="#000000"            android:textSize="18sp" />    </LinearLayout></LinearLayout>

然後再在Adapter的getView中去動態改變即可,關鍵代碼:

            if (view == null) {                view = getActivity().getLayoutInflater().inflate(                        R.layout.subject_list_item, null);                holder = new ViewHolder();                LinearLayout ll = (LinearLayout)view.findViewById(R.id.subject_ll);                holder.tvTitle = (TextView) view                        .findViewById(R.id.subject_title);                holder.imgIco = (ImageView) view.findViewById(R.id.subject_ico);                LayoutParams linearParams = (LayoutParams) ll                        .getLayoutParams();                linearParams.height = screenWidth * 250 / 460;//                linearParams.gravity = Gravity.CENTER_VERTICAL;                ll.setLayoutParams(linearParams);                view.setTag(holder);            } else {                holder = (ViewHolder) view.getTag();            }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.