【问题标题】:alignment of contents in ListViewListView 中内容的对齐方式
【发布时间】:2012-07-22 19:37:23
【问题描述】:

我有一个列表视图,我希望在左侧显示一些内容,在右侧显示其他内容: for example

表示内容的意思

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;
        }
}

问题是当我尝试parent.setPadding() 时,所有对象都在一侧的列表视图中对齐。

感谢您的宝贵时间 mkounal

我的内容1.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/objectscreenmoall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/content"
    android:clickable="true"
    android:onClick="btnListener"
    android:orientation="vertical"
    android:paddingRight="20dp"

    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/fromMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="@string/phoNum"
            android:textColor="#01acec"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/dateMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="@string/date"
            android:textColor="#01acec"
            android:textSize="15dp"
            android:layout_marginRight="10dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/bodyMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="@string/body"
            android:textColor="#01acec"
            android:textSize="15dp"
            android:layout_marginLeft="7dp" />

        <TextView
            android:id="@+id/statusMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:drawableRight="@drawable/ringtone_"
            android:layout_marginRight="10dp"
             />
    </RelativeLayout>

</LinearLayout>

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>

【问题讨论】:

    标签: android listview alignment adapter


    【解决方案1】:

    这里可以有两个不同的 XML。

    那是getItem(position).getIsMO() == false设置第一个布局的时候:

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

    其他

    convertView = mInflater.inflate(R.layout.mycontent2, null); // the row with green background.
    

    尝试这样做。

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

    【讨论】:

    • Shrikant 我编辑了我的问题。我发布了 2 个 mycontents 和调用 mycontents 的 ListView 中的一个。当我按照您的方式进行操作时,它会更改元素内的填充,但我想更改整个元素的大小和对齐方式,我做错了什么?求你的帮助
    • 你能详细说明你的问题吗?我没有完全了解情况。根据我的解决方案,您需要创建两个不同的 xml,在一个 xml 中,将所有视图放在左侧,在第二个 xml 中,将所有视图放在右侧。一旦你根据你的情况膨胀了正确的xml,那么就不需要填充了。
    • 我创建了两个 xml 文件,mycontent1.xml 和 mycontent2.xml,它们内部的代码相同,唯一的区别是我在左边第一个设置了 margin* 和右侧的第二个 xml 使用 android:layout_marginLeft="" 当我使用 inflate 方法调用这两个文件时,列表显示不同的元素,其中没有填充,一个接一个的像这样:(@ 987654321@)
    • 你的方式很好,谢谢你的变化很大,我只需要添加第二个 LinearLayout 以便在 2 个 xml 文件中做右边距。
    【解决方案2】:

    我想我会使用两种不同的行布局,一种是android:layout_gravity="right",另一种是android:layout_gravity="left"。然后只需检查您需要在适配器中使用哪个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-05
      • 2021-05-05
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2012-03-04
      相关资源
      最近更新 更多