2017-01-27 4 views
0

Ich zeige dynamisch eine Liste von Elementen in einer Tabelle an. Um sicherzustellen, dass jedes Element in der Tabelle gleichmäßig auf das übergeordnete Objekt verteilt ist, rufe ich .setLayoutParams() für jedes Element mit einem Gewicht von 0,33f an (es gibt drei Elemente in jeder Zeile). Die Artikel verteilen sich jedoch nicht gleichmäßig in der Reihe.Artikelgewicht wird in LinearLayout.LayoutParams nicht richtig festgelegt

Dies ist, was es leider wie folgt aussehen: enter image description here

Der Code für mein PaymentConfirmedActivity.java Paket 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); 
     } 

    } 
} 

Meine Aktivität 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> 

Wie kann ich sicherstellen, dass meine drei Textviews in jeder Reihe sind gleichmäßig gleichmäßig verteilt?

+1

Unrelated, aber immer noch: Sie müssen sagen, "bezahlt" statt "bezahlt" :) – Aenadon

Antwort

1

Die Art der layoutParams sollte immer die Art der Mutter der Ansicht, nicht die Ansicht selbst, also, wenn Ihr TableRow werden sollte TableLayout.LayoutParams sein in TableLayout seinen layoutParams hinzugefügt werden.

So sollte der Code wie folgt sein:

 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); 
Verwandte Themen