在 LinearLayout.LayoutParams 中未正确设置商品重量

Item weight not getting set properly in LinearLayout.LayoutParams

我正在 table 中动态显示项目列表。为了确保 table 中的每个项目在父对象中均匀分布,我对权重为 .33f 的每个项目调用 .setLayoutParams()(每行中有三个项目)。但是,这些项目仍然没有均匀地分布在行中。

不幸的是,这就是它的样子:

我的代码 PaymentConfirmedActivity.java 包裹 com.snapwebdevelopment.scanhappy;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.google.android.gms.vision.text.Text;
import com.snapwebdevelopment.scanhappy.firebasemodels.Product;
import com.snapwebdevelopment.scanhappy.managers.ShoppingListManager;

import java.util.List;

public class PaymentConfirmedActivity extends AppCompatActivity {

    private TableLayout purchasedItems;
    private List<Product> purchases;
    private ShoppingListManager mShoppingListManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment_confirmed);

        mShoppingListManager = ShoppingListManager.getInstance();
        purchases = mShoppingListManager.getProductList();

        Product p;


        purchasedItems = (TableLayout) findViewById(R.id.purchasedItems);
        for(Integer i = 0; i < purchases.size(); i++) {
            TextView purchaseName;
            TextView purchaseQuantity;
            TextView purchasePrice;

            p = purchases.get(i);

            TableRow row = new TableRow(this);
            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(
                    MATCH_PARENT,
                    MATCH_PARENT,
                    0.33f);

            row.setLayoutParams(layoutParams);

            purchaseName = new TextView(this);
            purchaseName.setText(p.getName());
            purchaseName.setTextColor(Color.WHITE);
            purchaseName.setLayoutParams(tvParams);
            row.addView(purchaseName);

            purchaseQuantity = new TextView(this);
            purchaseQuantity.setText(String.valueOf(p.getQuantity()));
            purchaseQuantity.setTextColor(Color.WHITE);
            purchaseQuantity.setLayoutParams(tvParams);
            row.addView(purchaseQuantity);

            purchasePrice = new TextView(this);
            purchasePrice.setText("$" + String.valueOf(p.getPrice()));
            purchasePrice.setTextColor(Color.WHITE);
            purchasePrice.setLayoutParams(tvParams);
            row.addView(purchasePrice);

            purchasedItems.addView(row, i);
        }

    }
}

我的activity布局

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_shopping_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:background="@color/colorPrimary"
    tools:context="com.snapwebdevelopment.scanhappy.PaymentConfirmedActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <include
            layout="@layout/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TableLayout
            android:id="@+id/purchasedItems"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="20dp"
            android:paddingTop="5dp"
            android:paddingRight="20dp"
            android:layout_weight="1" />

    </LinearLayout>

    <Button
        android:id="@+id/checkoutButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textColor="@color/colorButtonTextPrimary"
        android:background="@color/colorPrimary"
        android:text="Total Payed .12"
        android:shadowColor="@color/colorButtonShadow"/>

</RelativeLayout>

如何确保每行中的三个 TextView 均匀分布?

layoutParams 的类型应该始终是视图父视图的类型而不是视图本身,因此如果您的 TableRow 将被添加到 TableLayout 中,它的 layoutParams 应该是TableLayout.LayoutParams.

所以你的代码应该是这样的:

        TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
                MATCH_PARENT,
                MATCH_PARENT,
                0.33f);

        row.setLayoutParams(layoutParams);