如何将TableRow中的单元格与文本和图像对齐

时间:2018-05-02 13:38:41

标签: android android-layout

我有Table以编程方式填充TableRows。最后一行应包含文本(工资金额:" 45K")和文本旁边的图像。这样它们就在一起,文本和图像的结尾之间的空间最小。我已经制作了以下代码:

        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        TableRow.LayoutParams params1 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[6]);
        params1.gravity = Gravity.CENTER;
        linearLayout.setLayoutParams(params1);
        linearLayout.setGravity(Gravity.CENTER);

        TextView h1 = new TextView(context);
        h1.setText(SharedFunctions.makeSalaryString(scout.getSalary()));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, (float) 0.9);
        h1.setLayoutParams(params);
        h1.setTextColor(resources.getColor(standingsTableHeadTXT));
        h1.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);


        ImageView cash = new ImageView(context);
        params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, (float) 0.1);
        params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        cash.setLayoutParams(params);
        cash.setImageDrawable(resources.getDrawable(R.mipmap.usd_sign));

        linearLayout.addView(h1);
        linearLayout.addView(cash);
        tableRow.addView(linearLayout);

问题是它做了我想做的事,但整件事并没有集中:

example

将它们放在一起并同时居中的正确方法是什么?

  

编辑1:   如果我替换线       h1.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

通过     h1.setGravity(Gravity.CENTER);

我会得到这个:

example2

哪个不好,因为现金图像显示太远(应该如第一张图片中所示)。虽然它解决了中心对齐问题。

编辑2:用元素填充行的函数代码

public static TableRow createScoutRow(ScoutingPlayerJson scout, TableLayout tableLayout, Context context, Resources resources) {
    double[] weights = new double[]{0.1680, 0.2240, 0.1360, 0.0800, 0.0960, 0.1360, 0.1600};
    TableRow tableRow = new TableRow(context);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
    tableRow.setLayoutParams(lp);
    tableRow.setOrientation(TableRow.HORIZONTAL);
    tableRow.setBackgroundColor(resources.getColor(trainingPlayerSelectRowColor));
    tableRow.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    tableRow.setTag(scout.getId());

    {
        TextView h1 = new TextView(context);
        h1.setText(String.valueOf(scout.getCoins()));
        TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[0]);
        h1.setLayoutParams(params);
        h1.setTextColor(resources.getColor(standingsTableHeadTXT));
        h1.setGravity(Gravity.CENTER);
        h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        tableRow.addView(h1);
    }

    {
        TextView h1 = new TextView(context);
        h1.setText(scout.getFirstname().charAt(0) + ". " + scout.getSurname());
        TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[1]);
        h1.setLayoutParams(params);
        h1.setTextColor(resources.getColor(standingsTableHeadTXT));
        h1.setGravity(Gravity.LEFT);
        h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

        tableRow.addView(h1);
    }

    {
        TextView h1 = new TextView(context);
        h1.setText(scout.getAge().toString());
        TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[2]);
        h1.setLayoutParams(params);
        h1.setTextColor(resources.getColor(standingsTableHeadTXT));
        h1.setGravity(Gravity.CENTER);
        h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

        tableRow.addView(h1);
    }

    {
        TextView h1 = new TextView(context);
        h1.setText(scout.getBatThrowType().toString().substring(3));
        TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[3]);
        h1.setLayoutParams(params);
        h1.setTextColor(resources.getColor(standingsTableHeadTXT));
        h1.setGravity(Gravity.CENTER);
        h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

        tableRow.addView(h1);
    }
    {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        TableRow.LayoutParams params1 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[4]);
        linearLayout.setLayoutParams(params1);
        linearLayout.setGravity(Gravity.CENTER);
        Set<String> set = Utils.fixPrefferedPositions(scout.getPrefferedPosition());
        int mysize = set.size();

        for (String position: set) {
            View imview = getImageViewByPosition(position, mysize, context);
            linearLayout.addView(imview);
        }
        tableRow.addView(linearLayout);
    }
    { 
        ImageView imview = getImageViewByRating(scout.getRating(), (float) weights[5], context);
        tableRow.addView(imview);
    }
    {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        TableRow.LayoutParams params1 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[6]);
        params1.gravity = Gravity.CENTER;
        linearLayout.setLayoutParams(params1); 
        linearLayout.setGravity(Gravity.CENTER);

        TextView h1 = new TextView(context);
        h1.setText(SharedFunctions.makeSalaryString(scout.getSalary()));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, (float) 0.9);
        h1.setLayoutParams(params);
        h1.setTextColor(resources.getColor(standingsTableHeadTXT));
        //h1.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        h1.setGravity(Gravity.CENTER);
        h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);


        ImageView cash = new ImageView(context);
        params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, (float) 0.1);
        params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        cash.setLayoutParams(params);
        cash.setImageDrawable(resources.getDrawable(R.mipmap.usd_sign));

        linearLayout.addView(h1);
        linearLayout.addView(cash);
        tableRow.addView(linearLayout);
    }
    return tableRow;
}

定义表(添加行的表)的部分布局:

            <!--0.08    0.08    0.25    0.09    0.14    0.15    0.21-->
            <TableLayout
                android:id="@+id/scoutsTable"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:divider="@drawable/divider_stand_table_list"
                android:showDividers="middle"
                android:isScrollContainer="true"
                android:stretchColumns="7"
                android:orientation="horizontal">

            </TableLayout>

        </android.support.v4.widget.NestedScrollView>

1 个答案:

答案 0 :(得分:0)

尝试替换此行

h1.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

这一个

h1.setGravity(Gravity.CENTER);