我正在动态地显示表格中的项目列表。为了确保表中的每个项目均匀分布在父对象中,我在每个项目上调用.setLayoutParams()
,权重为.33f(每行有三个项目)。但是,这些物品仍然不能在排中均匀分散。
不幸的是,这就是它的样子: {{3}}
我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);
}
}
}
我的活动布局
<?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.12"
android:shadowColor="@color/colorButtonShadow"/>
</RelativeLayout>
如何确保每行中的三个TextView均匀分布均匀?
答案 0 :(得分:1)
layoutParams
的类型应始终是视图父级的类型,而不是视图本身的类型,因此,如果您的TableRow将添加到TableLayout
中,则其layoutParams
应为{{ 1}}。
所以你的代码应该是这样的:
TableLayout.LayoutParams