CustomDialog的Customview不会进入CustomList View的项目的中心

时间:2016-09-27 10:59:02

标签: android android-xml

我已经制作了自定义对话框,我使用下面的自定义Listview是代码

Dialog badgeListDialog = new Dialog(HomeActivity.this);
badgeListDialog.setContentView(R.layout.dialog_badge_list);

ListView lvBadgeList = (ListView) badgeListDialog.findViewById(R.id.lvBadgeList);
if(userBadgeListModel.getSuccess())
{
    List<Record> recordList = new ArrayList<>();
    recordList.add(record);
    lvBadgeList.setAdapter(new UserBadgeRecordListAdapter(HomeActivity.this, recordList));
}
badgeListDialog.show();

因为我需要两个充气机,所以适配器的代码是

public class UserBadgeRecordListAdapter extends BaseAdapter {

    List<Record> recordList = new ArrayList();
    LayoutInflater inflater;
    Context context;


    public UserBadgeRecordListAdapter(Context context, List<Record> myList) {
        this.recordList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        return recordList.size();
    }

    @Override
    public Record getItem(int position) {
            return recordList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder mViewHolder;
        ViewHolderFirst mViewHolderFirst;
        int type = getItemViewType(position);
        if (type == 0) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.dialog_badge_custom_progress_view_item, parent, false);
                mViewHolderFirst = new ViewHolderFirst();
                convertView.setTag(mViewHolderFirst);
            } else {
                mViewHolderFirst = (ViewHolderFirst) convertView.getTag();
            }

        } else {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.dialog_badge_list_item, parent, false);
                mViewHolder = new MyViewHolder(convertView);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (MyViewHolder) convertView.getTag();
            }


            Record currentListData = getItem(position);
            String badgeName = currentListData.getBadges();
            mViewHolder.tvBadgeName.setText(badgeName);
            mViewHolder.tvPointsToAchieve.setText(currentListData.getPointsToAchieve() + " Coins");

            if (badgeName != null && !badgeName.equals("")) {
                if (badgeName.equalsIgnoreCase("Bronze")) {
                    mViewHolder.ivBadgesIcon.setImageResource(R.drawable.ic_bronze);
                } else if (badgeName.equalsIgnoreCase("Silver")) {
                    mViewHolder.ivBadgesIcon.setImageResource(R.drawable.ic_silver);
                } else if (badgeName.equalsIgnoreCase("Gold")) {
                    mViewHolder.ivBadgesIcon.setImageResource(R.drawable.ic_gold);
                } else if (badgeName.equalsIgnoreCase("Platinum")) {
                    mViewHolder.ivBadgesIcon.setImageResource(R.drawable.ic_gold);
                } else if (badgeName.equalsIgnoreCase("Diamond")) {
                    mViewHolder.ivBadgesIcon.setImageResource(R.drawable.ic_bronze);
                }
            }
        }
        return convertView;
    }

    protected class ViewHolderFirst {
    }


    private class MyViewHolder {
        TextView tvBadgeName, tvPointsToAchieve;
        ImageView ivBadgesIcon;

        public MyViewHolder(View item) {
            tvBadgeName = (TextView) item.findViewById(R.id.tvBadgesName);
            tvPointsToAchieve = (TextView) item.findViewById(R.id.tvPointsToAchieve);
            ivBadgesIcon = (ImageView) item.findViewById(R.id.ivBadgesIcon);
        }
    }
}

dialog_badge_list.xml是

<?xml version="1.0" encoding="utf-8"?>

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lvBadgeList"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

dialog_badge_custom_progress_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:background="@color/black"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <View
        android:layout_width="match_parent"
        android:background="@android:color/holo_blue_bright"
        android:layout_height="1dp"/>
    <RelativeLayout
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:gravity="center">

        <com.insigmainc.view.DonutProgress
            android:id="@+id/donut_progress"
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:layout_marginTop="15dp"
            custom:donut_progress="30"
            custom:donut_text="Coins Earned" />

        <RelativeLayout
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:layout_marginTop="15dp">

            <TextView
                android:id="@+id/tvCoinsEaredText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Coins Earned"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tvCoinsEarned"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/tvCoinsEaredText"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                android:text="250"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tvYouNeedTo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tvCoinsEaredText"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:gravity="center_horizontal"
                android:text="You need 750 coins to\nreach Silver"
                android:textColor="@android:color/black" />
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>  

下面是Genymotion中的截图,它是完美的enter image description here

但Nexus-5和三星S7边缘的情况相同,如下所示

enter image description here

任何人都可以解决我的问题

谢谢,

0 个答案:

没有答案