Android ListView中内容的对齐

Android ListView中内容的对齐,android,listview,alignment,adapter,Android,Listview,Alignment,Adapter,我有一个listview,我希望列表的左侧显示一些内容,右侧显示其他内容: 意思是 getItem(position).getIsMO()==false 将显示在列表的左侧。以及 getItem(position).getIsMO()==true 将显示在列表的右侧 该部分的代码为: public static class EfficientAdapter extends ArrayAdapter<Message> implements Filter

我有一个listview,我希望列表的左侧显示一些内容,右侧显示其他内容:

意思是

getItem(position).getIsMO()==false

将显示在列表的左侧。以及

getItem(position).getIsMO()==true

将显示在列表的右侧

该部分的代码为:

    public static class EfficientAdapter extends ArrayAdapter<Message>
            implements Filterable {
        private LayoutInflater mInflater;
        // private Bitmap mIcon1;
        private final Context context;
        private final ArrayList<Message> values;

        public EfficientAdapter(Context context, ArrayList<Message> values) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            super(context, R.layout.mycontent, values);
            mInflater = LayoutInflater.from(context);
            this.context = context;
            this.values = values;
        }

        /**
         * Make a view to hold each row.
         * 
         * @see android.widget.ListAdapter#getView1(int, android.view.View,
         *      android.view.ViewGroup)
         */
        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid
            // unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;
            // When convertView is not null, we can reuse it directly, there is
            // no need
            // to reinflate it. We only inflate a new View when the convertView
            // supplied
            // by ListView is null.

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.mycontent, null);

                holder = new ViewHolder();
                if (getItem(position).getIsMO() == false) {
                    // parent.setPadding(0, 0, 15, 0);
                    // convertView.layout(15, 0, 0, 0);
                    holder.body = (TextView) convertView
                            .findViewById(R.id.body);
                    holder.date = (TextView) convertView
                            .findViewById(R.id.date);
                    holder.from = (TextView) convertView
                            .findViewById(R.id.from);
                    holder.status = (TextView) convertView
                            .findViewById(R.id.status);
                } else {
                    // convertView.layout(0, 0, 15, 0);
                    // parent.setPadding(15, 0, 0, 0);

                    holder.body = (TextView) convertView
                            .findViewById(R.id.body);
                    holder.date = (TextView) convertView
                            .findViewById(R.id.date);
                    holder.from = (TextView) convertView
                            .findViewById(R.id.from);
                    holder.status = (TextView) convertView
                            .findViewById(R.id.status);
                }
                convertView.setTag(holder);
            }

            else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            convertView.setOnClickListener(new OnClickListener() {
                private int pos = position;

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Click-" + pos, Toast.LENGTH_SHORT)
                            .show();
                }
            });

            holder.body.setText(getItem(position).getBody());
            holder.date.setText(getItem(position).getDate());
            holder.from.setText(getItem(position).getPhoneNumber());
            holder.status
                    .setText(String.valueOf(getItem(position).getStatus()));
            /*
             * if ("000".equals(getItem(position).getPhoneNumber())) { //
             * convertView.setPadding(5, 0, 0, 0); // View layout =
             * View.inflate(context, R.layout.mycontent, null); //
             * layout.setPadding(5, 0, 0, 0);
             * convertView.setLayoutParams(params); // parent.setPadding(5, 0,
             * 0, 0); } else { // View layout = View.inflate(context,
             * R.layout.mycontent, null); // layout.setPadding(0, 0, 5, 0);
             * parent.setPadding(0, 0, 5, 0); // convertView.setPadding(0, 0, 5,
             * 0); }
             */
            return convertView;

        }

        static class ViewHolder {
            TextView from;
            TextView date;
            TextView body;
            TextView status;
        }
}
ListView.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/allList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listall"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="2"
        android:divider="#000000"
        android:dividerHeight="2dp"
        android:footerDividersEnabled="true"
         >
    </ListView>

</LinearLayout>

这里可以有两个不同的XML

这就是当
getItem(position).getIsMO()==false
设置第一个布局时,如下所示:

convertView = mInflater.inflate(R.layout.mycontent1, null); // the row with blue background.
否则

试着这样做


如果这不起作用,我们会找到其他解决方案。

我想我会使用两种不同的行布局,一种是带有
android:layout\u gravity=“right”
,另一种是带有
android:layou gravity=“left”
。然后只需检查适配器中需要使用哪一个。

Shrikant我编辑了我的问题。我发布了来自2个mycontent和调用mycontent的ListView的一个。当我按照你的方式做时,它会改变元素内部的填充,但是我想改变整个元素的大小和对齐方式,我做错了什么?谢谢你能详细说明你的问题吗?我没有完全理解这个场景。根据我的解决方案,您需要创建两个不同的xml,其中在一个xml中,将所有视图放在左侧,在第二个xml中,将所有视图放在右侧。一旦您根据自己的情况膨胀了正确的xml,那么就不需要填充了。我创建了两个xml文件,mycontent1.xml和mycontent2.xml,它们内部有相同的代码,唯一的区别是我使用android:layout_marginLeft=“”,在左边的第一个xml中设置了边距*,在右边的第二个xml中设置了边距。当我使用充气方法调用这两个文件时,列表中会显示不同的元素,就像它们没有填充一样,一个接一个这样:()你的方式很好,谢谢你变化很大,我只需要添加第二个LinearLayout,以便在2个xml文件中获得正确的边距。
convertView = mInflater.inflate(R.layout.mycontent1, null); // the row with blue background.
convertView = mInflater.inflate(R.layout.mycontent2, null); // the row with green background.