動態設定ViewPager的高度

2021-06-27 20:23:59 字數 1522 閱讀 2855

當viewpager只是作為元素在乙個頁面時,他的高度設定使用match_parent和wrap_content都沒有效果,最後的結果就是顯示不出來,必須指定固定高度才能顯示出來,但是不會自適應,解決辦法:

首先是重寫viewpager的onmeasure方法:

package com.yang.demo;

import android.content.context;

import android.support.v4.view.viewpager;

import android.util.attributeset;

import android.util.log;

import android.view.view;

public class wrapcontentheightviewpager extends viewpager

/*** * constructor * * @param context the context * @param attrs the attribute

* set

*/public wrapcontentheightviewpager(context context, attributeset attrs)

@override

protected void onmeasure(int widthmeasurespec, int heightmeasurespec)

heightmeasurespec = measurespec.makemeasurespec(height,

measurespec.exactly);

super.onmeasure(widthmeasurespec, heightmeasurespec);

}}

這個時候設定viewpager高度為wrap_content,會看到只顯示listview中的乙個元素出來,問題是listview的高度也沒有自適應內容,這個時候就需要去動態改變listview的高度。

方法:

public void setlistviewheightbasedonchildren(listview listview) 

int totalheight = 0;

for (int i = 0; i < listadapter.getcount(); i++)

viewgroup.layoutparams params = listview.getlayoutparams();

params.height = totalheight

+ (listview.getdividerheight() * (listadapter.getcount() - 1));

((marginlayoutparams) params).setmargins(10, 10, 10, 10); // 可刪除

listview.setlayoutparams(params);

}

這個方法要在listview設定完adapter之後使用。

動態設定viewport的寬高

先貼 see the pen egpdo by 劉志剛 liuzhigang on codepen.demo中需要了解的模組 html中id是stage的div是遊戲總容器,js中的setviewport函式作用是設定視口寬高 css樣式主要是為了讓stage元素在手機瀏覽器中豎直和水平居中 需要了...

js css style動態設定table高度

設定table高度想必大家都會,直接在table標籤中設定下不就行了嗎?這是靜態的,如果要動態設定你會嗎?下面的例項將教會大家直接在table標籤中設定下不就行了嗎?這是靜態的,如果要動態設定你會嗎?function com onresize document.getelementbyid tabl...

viewPager滾動速度的設定

viewpager 滾動速度設定 public class viewpagerscroller extends scroller public viewpagerscroller context context public viewpagerscroller context context,int...