Android在最长路径上垂直对齐listview元素

Android在最长路径上垂直对齐listview元素,android,listview,tablerow,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="@c

我已经为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>
它由左侧的图标和右侧的文本组成。文本在左侧对齐,listview的所有元素都垂直对齐

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

我试图在初始化时膨胀ArrayAdapter中的行,以找出最长的行,但我总是得到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>

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

如果您无法与客户争论,那么唯一能满足客户要求的方法就是制作自定义组件。
/**
 * 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;
}
    // 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);