【问题标题】:tablelayout tablerow layout alignmenttablelayout tablerow 布局对齐
【发布时间】:2022-03-13 17:04:03
【问题描述】:

我完全不知道为什么 test,test1,test2,test3 与第一行不正确对齐。第一行是指 test,2012,test,test。我希望下一行跟在表格的第一行之后。

xml:

 <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tl"
        android:weightSum="5"
        >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#27160e" >
    
       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textStyle="bold"
            android:textSize="18sp"
            android:gravity="center"
            android:textColor="#fad06e"
            android:layout_weight="1"
            android:text="Project" />
       
       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textStyle="bold"
            android:textSize="18sp"
            android:gravity="center"
            android:textColor="#fad06e"
            android:layout_weight="1"
            android:text="Project2" />
       
         <TableLayout
             android:layout_weight="3"
        android:layout_marginRight="4dp"
       android:stretchColumns="*"
       >
       
             
       
       <TableRow>
       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="#fad06e"
            android:text="Cost"
            android:gravity="center"
            android:layout_span="2" />
   </TableRow>
        <TableRow
            >
        
        
      
         
       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="#fad06e"
            android:text="2011" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="#fad06e"
            android:text="2012" />
                  </TableRow>
                  </TableLayout>
    </TableRow>

  <TableRow>
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
          
            android:layout_weight="1"
          
            android:text="test" />
      <TableLayout android:stretchColumns="*"
          android:layout_weight="3">
          <TableRow>
                <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
           
            android:text="test" />
              
          </TableRow>
          
      </TableLayout>
      
  </TableRow>

   
    
</TableLayout>

代码:

TableLayout tl;
        
        tl = (TableLayout) findViewById(R.id.tl);
        
        for(int x = 0; x < 3; x++) {
            
            // Create the table row
            TableRow tr = new TableRow(this);
            tr.setId(100+x);

            TextView lblrecID = new TextView(this);
            lblrecID.setId(200+x);
            lblrecID.setText("test");
            lblrecID.setGravity(Gravity.CENTER); 
            LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
            lblrecID.setLayoutParams(params);
            tr.addView(lblrecID);// add the column to the table row here

            TextView lblrecName = new TextView(this);
            lblrecName.setId(200+x);// define id that must be unique
            lblrecName.setText("test1");
            lblrecName.setGravity(Gravity.CENTER);
            lblrecName.setLayoutParams(params);
            tr.addView(lblrecName); // add the column to the table row here
            
            TableLayout tl1 = new TableLayout(this);
            LayoutParams params1 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
            tl1.setStretchAllColumns(true);
            tl1.setLayoutParams(params1);
            TableRow tr2 = new TableRow(this);
            
            TextView lblrecID1 = new TextView(this);
            lblrecID1.setId(200+x);
            lblrecID1.setText("test2");
            lblrecID1.setGravity(Gravity.CENTER); 
            tr2.addView(lblrecID1);// add the column to the table row here

            TextView lblrecName1 = new TextView(this);
            lblrecName1.setId(200+x);// define id that must be unique
            lblrecName1.setText("test3");
            lblrecName1.setGravity(Gravity.CENTER);
            tr2.addView(lblrecName1); // add the column to the table row here
            tl1.addView(tr2);     
            // finally add this to the table row
            
            tr.addView(tl1);
            //tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            
            
        }

结果

【问题讨论】:

    标签: android tablelayout tablerow


    【解决方案1】:

    好的。我没有得到你想要的。大概是这样的布局吗?

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tl"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*">
    
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#27160e" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Project"
            android:textColor="#fad06e"
            android:textSize="18sp"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Project2"
            android:textColor="#fad06e"
            android:textSize="18sp"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Cost"
            android:textColor="#fad06e"
            android:textSize="18sp"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="2011"
            android:textColor="#fad06e"
            android:textSize="18sp"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="2012"
            android:textColor="#fad06e"
            android:textSize="18sp"
            android:textStyle="bold" />
    </TableRow>
    
    <TableRow>
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    </TableRow>
    
     <TableRow>
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test" />
    </TableRow>
    

    【讨论】:

    • 编辑了我的答案。请更具体。你想要什么? Cost 列应该为空还是什么?
    • 对齐不正确。测试能否与测试列对齐,test1与2012列对齐等等...
    猜你喜欢
    • 2014-10-01
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多