将表格内容与Android中的标题对齐

时间:2016-02-17 13:02:38

标签: android layout tablelayout

我试图将表格(由代码创建)的内容与标题对齐。 到目前为止,我已尝试使用砝码,但它似乎无法正常工作

这是结果 enter image description here

的活动:

import ngs.ariesmobile.AriesMobileActivity;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.commons.codec.digest.UnixCrypt;

import ngs.ariesmobile.R;
import ngs.ariesmobile.R.layout;
import ngs.ariesmobile.data.Customer;
import ngs.ariesmobile.data.Report;
import ngs.ariesmobile.data.System;
import ngs.ariesmobile.data.Ticket;
import android.R.integer;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.CalendarContract.Colors;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class SystemStoricalActivity extends AriesMobileActivity {

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

        float[] weights = new float[] { 0.2f, 0.2f, 0.2f, 0.4f};

        TableLayout tlSystemStorical= (TableLayout) findViewById(R.id.tl_system_storical);
        TableRow tr_head = new TableRow(this);

        Bundle extras = getIntent().getExtras();
        String s=null;
        int system_id=0;
        if (extras != null) {
            s = extras.getString("id_for_storical");

        system_id=Integer.parseInt(s);
    }
    ngs.ariesmobile.data.Report[] reportList= new ngs.ariesmobile.data.Report.Provider(context).findBySystem(system_id);

    Report r=null;
    for(int i=0;i<reportList.length-1;i++){
        int min=i;
        for(int j=i+1; j<reportList.length;j++){
            if(reportList[min].getYear()<reportList[j].getYear() ||(reportList[min].getYear()==reportList[j].getYear() && reportList[min].getId()<reportList[j].getId())){
                min=j;
            }
        }
        if(min!=i){
            r=reportList[min];
            reportList[min]=reportList[i];
            reportList[i]=r;
        }
    }

    tr_head.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT));

    tr_head.setBackgroundColor(Color.rgb(0, 230, 230));

    TextView lblSystemStoricalHeaderId = new TextView(this);
    lblSystemStoricalHeaderId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0]));
    lblSystemStoricalHeaderId.setText("ID");

    tr_head.addView(lblSystemStoricalHeaderId);

    TextView lblSystemStoricalHeaderData = new TextView(this);
    lblSystemStoricalHeaderData.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[1]));
    lblSystemStoricalHeaderData.setText("Data");

    tr_head.addView(lblSystemStoricalHeaderData);

    TextView lblSystemStoricalHeaderType = new TextView(this);
    lblSystemStoricalHeaderType.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[2]));
    lblSystemStoricalHeaderType.setText("Tipo");

    tr_head.addView(lblSystemStoricalHeaderType);

    TextView lblSystemStoricalHeaderDescription = new TextView(this);
    lblSystemStoricalHeaderDescription.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[3]));
    lblSystemStoricalHeaderDescription.setText("Descrizione");

    tr_head.addView(lblSystemStoricalHeaderDescription);

    tr_head.setMinimumHeight(30);

    tlSystemStorical.addView(tr_head, new TableLayout.LayoutParams(
       LayoutParams.MATCH_PARENT,
           LayoutParams.WRAP_CONTENT));

    for(int i=0;i<reportList.length;i++){

        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        TextView lblSystemStoricalId = new TextView(this);
        lblSystemStoricalId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0]));
        lblSystemStoricalId.setText(""+reportList[i].getId()+"");

        tr.addView(lblSystemStoricalId);

        TextView lblSystemStoricalData = new TextView(this);
        lblSystemStoricalData.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[1]));
        long dataC = reportList[i].getDate()*1000;
        java.util.Date df = new java.util.Date(dataC);
        String vv = new SimpleDateFormat("dd/MM/yyyy").format(df);
        lblSystemStoricalData.setText(vv);

        tr.addView(lblSystemStoricalData);

        TableRow.LayoutParams tv3Params = new TableRow.LayoutParams();
        tv3Params.width=0;
        tv3Params.weight=1;
        TextView lblSystemStoricalType = new TextView(this);
        lblSystemStoricalType.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[2]));
        int systemId = reportList[i].getSystemId();
        System system = new ngs.ariesmobile.data.System.Provider(context).findById(systemId);
        lblSystemStoricalType.setText(""+system.getType());
        lblSystemStoricalType.setLayoutParams(tv3Params);

        tr.addView(lblSystemStoricalType);

        TableRow.LayoutParams tv4Params = new TableRow.LayoutParams();
        tv3Params.width=0;
        tv3Params.weight=1;
        TextView lblSystemStoricalDescription = new TextView(this);
        lblSystemStoricalDescription.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,weights[3]));
        lblSystemStoricalDescription.setText(""+reportList[i].getReportDescription());
            lblSystemStoricalDescription.setLayoutParams(tv3Params);

            tr.addView(lblSystemStoricalDescription);

            tr.setBackgroundResource(R.drawable.border);
            tr.setMinimumHeight(40);

            tlSystemStorical.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
    }
}

布局:

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">



    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scroll_view_system_description"
         >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
             >

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*"
                android:id="@+id/tl_system_storical"
                >

            </TableLayout>
        </FrameLayout>

    </ScrollView>


</RelativeLayout>

我也试图修改权重值,但似乎没有任何区别。

提前致谢

0 个答案:

没有答案