我动态地在表格中显示项目列表。 为了确保表中的每个项目均匀分布在父对象中,我在每个项目上调用.setLayoutParams() ,权重为.33f(每行有三个项目)。 但是,这些物品仍然不能在排中均匀分散。
不幸的是,这就是它的样子:
我的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均匀均匀展开?
I'm displaying a list of items in a table dynamically. In order to make sure each item in the table is evenly spread out among the parent object, I am calling .setLayoutParams() on each item with a weight of .33f (there are three items in each row). However, the items continue to not disperse evenly among the row.
This is what it, unfortunately, look like:
The code for my PaymentConfirmedActivity.java package 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); } } }My activity layout
<?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>How do I make sure that my three TextViews in each row are evenly spread out evenly?
最满意答案
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);The type of layoutParams should always be the type of the parent of the view not the view itself, so if your TableRow will be added in TableLayout its layoutParams should be TableLayout.LayoutParams.
So your code should be like this :
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);更多推荐
发布评论