0

我已經知道這個問題以前已經問過,但沒有一個解決方案似乎適用於我。textView的文本保留在一行

在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); 
     } 

    }); 

} 

} 

回答

0

如果您堅持使用TableLayout,你可以設置權重,最後TextView

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

但你也可以擺脫TableLayout的,只需使用一個容器,你一直爲每個加入LinearLayouts當前行,權重設置爲期望的值。

+0

工作感謝:) – Inoshichi

+0

很好聽!接受答案按鈕是否也適用? ;-) –

0

你爲什麼要使用按鈕,而不是TextView的顯示在屏幕上的文字?默認情況下,文本視圖是一個多線組件,您不必做任何事情就可以使文本出現在多行中。