[英]Android: How to set Item ListView height in percentage?
嗨,Android開發人員,
我是Android開發的新手,現在正在編寫我的第一個Android應用程序。 但是,我有一個我不知道的問題。
情況:我已經實現了ListFragment,並且ItemAdapter擴展了ArrayAdapter。 當用戶單擊片段中的按鈕時,此適配器總是返回五個或更少的項。
問題是如何將項目高度設置為父元素的20%? 我嘗試使用layout_height =“ 0dp”將layout_weight設置為布局項目的20,但這不能解決我的問題。 如果無法以百分比設置高度,是否可以通過編程方式獲取父元素的高度並設置項目高度?
片段XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/grey_light"
tools:context=".Fragment" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/footer"
android:isScrollContainer="false">
</ListView>
<RelativeLayout
android:id="@+id/footer"
android:layout_gravity="bottom"
style="@style/footer" >
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/button.next"
android:textSize="@dimen/text_bigger"
android:textColor="@color/white"
android:drawableRight="@drawable/ic_chevron_white"
android:background="@android:color/transparent" />
</RelativeLayout>
</FrameLayout>
片段類onActivityCreated方法:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MyActivity activity = (MyActivity)getActivity();
ItemAdapter adapter = new ItemAdapter (getActivity(), activity.getNextItems(5));
setListAdapter(adapter);
}
項目XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:layout_weight="80"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/txtV_order"
android:text="@+id/txt_order"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textColor="?darkColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/txtV_text"
android:text="@+id/txt_text"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/txtV_order"
android:layout_marginLeft="24dp"
android:textColor="?darkColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_weight="20"
android:layout_width="0dp"
android:layout_height="match_parent"
android:padding="@dimen/standard_padding">
<ToggleButton
android:id="@+id/btn_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroud="@drawable/toggle"
android:layout_centerInParent="true" />
</RelativeLayout>
</LinearLayout>
ItemAdapter類的getView方法:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.txtV_order);
textView.setText(Integer.toString(position + 1));
textView = (TextView) rowView.findViewById(R.id.txtV_text);
textView.setText(item[position].getText());
ToggleButton btn = (ToggleButton) rowView.findViewById(R.id.btn_toggle);
//if true - set checked if false - set unchecked
btn.setChecked(item[position].isChecked());
onToggleBtnClick(btn, position);
return rowView;
}
解:
我的實用程序類:
public class Utils {
//get status bar height
public static int getStatusBarHeight(Activity activity) {
int result = 0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = activity.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
//get action/title bar height
public static int getActionBarHeight(Activity activity) {
Rect rect= new Rect();
Window window= activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight= rect.top;
int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
return contentViewTop - statusBarHeight;
}
//get screen height with bars
public static int getScreenHeight(Activity activity) {
WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
return height;
}
//get height of layout...
public static int getLayoutHeight(Activity activity) {
return getScreenHeight(activity) - getActionBarHeight(activity) - getStatusBarHeight(activity);
}
}
在ItemAdapter類中:
View rowView = inflater.inflate(R.layout.item, parent, false);
int numberOfItems = 5;
int itemHeight = (int)(Utils.getLayoutHeight(context) / numberOfItems);
rowView.setMinimumHeight(itemHeight);
有兩種選擇:
在linearlayoutout中的xml中為2設置listView的android:weightsum=10
和android:layoutweight=8
LayoutWeight :這是一個不錯的鏈接,供您理解。
在我的問題中,我提供了一種解決方案,可根據需要在布局中填充項目數量並以編程方式設置項目高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.