的TextView'文字保持在一行

时间:2015-08-03 12:27:05

标签: android-layout android-activity textview android-tablelayout android

我已经知道之前已经提出过这个问题,但似乎没有一个解决方案适合我。

在srollView中我有一个表格布局,我在每行的最后一个单元格中添加三列我有一个大文本,应该在多行中自动格式化,但文本保持在屏幕外的一行。

问题是:当他到达屏幕末尾时,如何设置代码中的文本?

这是我的代码:

<?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">

         <RelativeLayout
             android:id="@+id/buttons_up_system_storical_layout"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentTop="true"
             android:gravity="left"
             android:orientation="horizontal" >

             <Button
                 android:id="@+id/btn_system_to_technical_part"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Parte tecnica" />

             <Button
                 android:id="@+id/btn_system_to_description"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentRight="true"
                 android:text="Descrizione" />

             <Button
                 android:id="@+id/btn_system_to_ticket"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_toLeftOf="@+id/btn_system_to_description"
                 android:layout_alignParentTop="true"
                 android:text="Vai a ticket" />

             <Button
                 android:id="@+id/btn_system_new_report"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_toRightOf="@+id/btn_system_to_technical_part"
                 android:text="Nuovo rapporto" />

         </RelativeLayout>

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

            <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>

代码:

public class SystemStoricalActivity extends Activity {

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

    Context context=this;
    TableLayout tlSystemStorical= (TableLayout) findViewById(R.id.tl_system_storical);
    TableRow tr_head = new TableRow(this);
    Button btnSystemToDescription = (Button) findViewById(R.id.btn_system_to_description);
    Button btnSystemNewReport = (Button) findViewById(R.id.btn_system_new_report);
    Button btnSystemToTicket = (Button) findViewById(R.id.btn_system_to_ticket);
    Button btnSystemToTechnicalPart = (Button) findViewById(R.id.btn_system_to_technical_part);

    Bundle extras = getIntent().getExtras();
    String s=null;
    if (extras != null) {
        s = extras.getString("ticket_from_system");
    }

    final Customer[] customers = new Customer.Provider(context).searchBySystemIdOrCompanyName(s);
    Customer c= customers[0]; 
    ngs.ariesmobile.data.System[] a  = new ngs.ariesmobile.data.System.Provider(context).findByCustomer(c);
    ngs.ariesmobile.data.Report[] reportList= new ngs.ariesmobile.data.Report.Provider(context).findByCustomer(c);

        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.setText("ID      ");

    tr_head.addView(lblSystemStoricalHeaderId);

    TextView lblSystemStoricalHeaderData = new TextView(this);
    lblSystemStoricalHeaderData.setText("Data");

    tr_head.addView(lblSystemStoricalHeaderData);

    TextView lblSystemStoricalHeaderDescription = new TextView(this);
    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.setText("  "+reportList[i].getId()+"  |  ");

        tr.addView(lblSystemStoricalId);

        TextView lblSystemStoricalData = new TextView(this);
        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);

        TextView lblSystemStoricalDescription = new TextView(this);
        lblSystemStoricalDescription.setText("  |  "+reportList[i].getReportDescription());

        tr.addView(lblSystemStoricalDescription);

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

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


    btnSystemToDescription.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Bundle si=getIntent().getExtras();
            String s=si.getString("ticket_from_system");
            Intent intent = new Intent(getApplicationContext(), SystemActivity.class);
            intent.putExtra("ticket_from_system",s);
            startActivity(intent);
        }

    });

    btnSystemNewReport.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Bundle si=getIntent().getExtras();
            String s=si.getString("ticket_from_system");
            Intent intent = new Intent(getApplicationContext(),ReportCustomerActivity.class);
            intent.putExtra("report_from_system",s);
            startActivity(intent);
        }

    });

    btnSystemToTicket.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Bundle si=getIntent().getExtras();
            String s=si.getString("ticket_from_system");
            Intent intent = new Intent(getApplicationContext(), TicketListActivity.class);
            intent.putExtra("ticket_from_system",s);
            startActivity(intent);
        }

    });

    btnSystemToTechnicalPart.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Bundle si=getIntent().getExtras();
            String s=si.getString("ticket_from_system");
            Intent intent = new Intent(getApplicationContext(), SystemTechnicalPartActivity.class);
            intent.putExtra("ticket_from_system",s);
            startActivity(intent);
        }

    });

}

}

2 个答案:

答案 0 :(得分:0)

为什么使用Button而不是TextView在屏幕上显示文字?默认情况下,文本视图是一个mutliline小部件,您无需执行任何操作即可使文本显示在多行中。

答案 1 :(得分:0)

如果您坚持使用TableLayout,则可以将权重设置为上一个TextView

TableRow.LayoutParams text3Params = new TableRow.LayoutParams();
text3Params.width = 0;
text3Params.weight = 1;
lblSystemStoricalDescription.setLayoutParams(text3Params);

但你也可以摆脱TableLayout并只使用一个容器,为每个当前行添加LinearLayouts,并将权重设置为所需的值。