Android垂直对齐listview元素最长

时间:2014-06-17 17:11:18

标签: android listview tablerow

我为listView

创建了这个布局
<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/icon_row"
    android:background="@color/icon_and_name_background"
    android:orientation="horizontal"
    android:gravity="center">

    <TableRow>

        <ImageView
            android:id="@+id/icon"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="2">

            <com.Wonderland.graphicObjects.MyTextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textColor="@color/Alice_Title"
                android:textSize="20sp"
                android:layout_alignParentLeft="true" />

        </RelativeLayout>
    </TableRow>
</TableLayout>

它由左边的图标和右边的文字组成。文本在左侧是对齐的,列表视图的所有元素都是垂直对齐的。

客户希望水平居中的文本最长的行(图标和文本)以及所有其他行必须与此对齐。

我尝试在初始化时对ArrayAdapter中的行进行充气,以找出哪个是最长的,但我总是为0。

我如何满足客户的需求?

1 个答案:

答案 0 :(得分:0)

我解决了在图像左侧为每一行设置自定义边距的问题。

新行xml:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/icon_row"
    android:background="@color/icon_and_name_background"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <ImageView
            android:id="@+id/icon"
        android:layout_width="@dimen/icon_width"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />


    <com.Wonderland.graphicObjects.MyTextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textColor="@color/Alice_Title"
                android:textSize="20sp"
        android:layout_alignParentLeft="true"
        android:paddingLeft="40px" />


</LinearLayout>

现在所有图像都具有相同的宽度,因此更容易对齐每个元素。

在自定义ArrayAdapter中,我添加了此代码来计算左边距

/**
 * Method to calculate the left margin of the object to center the longest
 */
private void calculateLeftMargin() {

    // reset the left margin
    marginLeft = 0;

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    // lenght of the screen
    int width = size.x;
    int height = context.getResources().getDimensionPixelSize(R.dimen.icon_row);

    LinearLayout linearLayout = new LinearLayout(context);
    View row = inflateRow(linearLayout);

    ImageView icon = (ImageView) row.findViewById(R.id.icon);
    TextView name = (TextView) row.findViewById(R.id.name);

    // max length of the object
    int maxLength = 0;

    for (int i = 0; i < getCount(); i++) {

        Character c = getItem(i);

        icon.setImageDrawable(c.getDrawableImage(context));
        name.setText(c.getName());

        row.measure(width, height);

        int max = icon.getMeasuredWidth() + name.getMeasuredWidth();

        if (maxLength < max)
            maxLength = max;
    }

    // margin left to center the longest object
    marginLeft = (width - maxLength) / 2;
}

我创建了一个自定义的LinearLayout对象,在其中填充了arrayAdapter的资源,并强制Android通过 row.measure(width,height); 来计算度量。在所有宽度中,我保持最长的计算边距,水平居中最长的行。

getView 方法中,在返回行上方,我输入了以下代码:

    // Update the margin left of every row to align to the longest object
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(holder.icon.getLayoutParams());
    params.setMargins(marginLeft, 0, 0, 0);
    holder.icon.setLayoutParams(params);

此代码将每个图像左边的边距更新为之前找到的值,对齐所有行。

相关问题