嗨,Android开发者,
我是Android开发中的新手,现在我正在编写我的第一个Android应用程序。 但是,我有一个我无法弄清楚的问题。
情况: 我实现了ListFragment,ItemAdapter扩展了ArrayAdapter。 当用户单击片段中的按钮时,此适配器始终返回五个或更少的项目。
问题是如何将项目高度设置为父元素的20%? 我尝试将layout_weight设置为布局项目的20,其中layout_height =" 0dp"但这没有解决我的问题。 如果设置高度百分比不可能,我可以获得父元素的高度并以编程方式设置项目高度吗?
片段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;
}
SOLUTION:
我的Util类:
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);
答案 0 :(得分:0)
有两种选择:
for linearlayout for xml in linearlayout为listView设置android:weightsum=10
和android:layoutweight=8
LayoutWeight:这是一个很好的链接,供您理解。
答案 1 :(得分:0)
在我的问题中,帖子是我用项目数设置项目高度的解决方案,我想在布局中填写它。