开发者

ListView height (actual)

开发者 https://www.devze.com 2023-01-24 09:56 出处:网络
There is a list adapter: public class timetable_list_adapter extends BaseAdapter { ... } and dynamic added lists:

There is a list adapter:

public class timetable_list_adapter extends BaseAdapter 
{
...
}

and dynamic added lists:

timetable_list_adapter adapter = new timetable_list_adapter(getApplicationContext(), eventList);
week_day_list.setAdapter(adapter);

list element is TableLayout

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_root_table"
android:layout_widt开发者_JAVA百科h="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="2">

In result application height of ListViews is wrong. I tried to set height:

enter code hep = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT); 
TableLayout tl = (TableLayout)findViewById(R.id.item_root_table); 
p.height = 60 * eventList.size(); // INFO: change it after list_itemre

It works, BUT! on the other screen(smaller than mine) lists are too big. What should I do?


p.height = 60 * eventList.size();

Those are absolute pixels, depending on the density of the screen they will end up the wrong size.

Try this:

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);    
DisplayMetrics metrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metrics);

p.height = 60 * eventList.size() * metrics.density;

This will turn the absolute pixels into density independent pixels (dip), so it should be the same physical size on every screen.

0

精彩评论

暂无评论...
验证码 换一张
取 消