我实现了一个自定义listView,如下所示: http://www.tutecentral.com/android-custom-navigation-drawer/但我无法为列表中的商品获得正确的高度。标题和部分布局都太大(高度)。
problem item height http://i61.tinypic.com/x1nfyd.png
这是我的xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/profileLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp"
>
<com.facebook.widget.ProfilePictureView
android:id="@+id/drawer_profilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
<TextView
android:id="@+id/drawer_profileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/button_logout"
android:text="Logout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/sectionLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
>
<TextView
android:id="@+id/drawer_sectionName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_marginTop="0dp"
>
<TextView
android:id="@+id/drawer_headerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:background="#DADADC" ></View>
</LinearLayout>
有什么建议吗?
[编辑]这是我的customDrawerAdapter getview代码:
public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {
private final JSONObject userProfile;
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID,
List<DrawerItem> listItems, JSONObject userProfile) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
this.userProfile = userProfile;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.profileName = (TextView) view
.findViewById(R.id.drawer_profileName);
drawerHolder.profilePicture = (ProfilePictureView) view.findViewById(R.id.drawer_profilePicture);
drawerHolder.sectionName = (TextView) view.findViewById(R.id.drawer_sectionName);
drawerHolder.headerName = (TextView) view.findViewById(R.id.drawer_headerName);
drawerHolder.buttonLogout = (Button) view.findViewById(R.id.button_logout);
drawerHolder.buttonLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ParseUser.logOut();
Intent intent = new Intent(getContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
}
});
drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
drawerHolder.sectionLayout = (LinearLayout) view.findViewById(R.id.sectionLayout);
drawerHolder.headerLayout = (LinearLayout) view.findViewById(R.id.headerLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = this.drawerItemList.get(position);
if (dItem.isHeader()) {
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.headerName.setText(dItem.getHeaderName());
} else if (dItem.getProfilePicture() == 0){
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionName.setText(dItem.getSectionName());
} else {
drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
try {
if (userProfile.getString("facebookId") != null) {
String facebookId = userProfile.get("facebookId")
.toString();
drawerHolder.profilePicture.setProfileId(facebookId);
} else {
// Show the default, blank user profile picture
drawerHolder.profilePicture.setProfileId(null);
}
if (userProfile.getString("name") != null) {
drawerHolder.profileName.setText(userProfile.getString("name"));
} else {
drawerHolder.profileName.setText("");
}
} catch (JSONException e) {
Log.d(CarpoolMeApp.TAG,
"Error parsing saved user data.");
}
}
return view;
}
答案 0 :(得分:1)
您可以手动为headerName和sectionName Textviews
设置高度 android:layout_height="2dp"
就像你在headerLayout中使用View一样
答案 1 :(得分:0)
您可以指定布局重力,这是问题的最可能原因(高度太高)。 您也可以使用LinearLayout作为根布局而不是RelativeLayout。 如果需要确切的高度,请在 layout_height 参数中指定。