2017-03-10 16 views
1

我想創建一個帶有凍結標題的表。我可以通過使用包含另一個tablelayout的scrollview的tablelayout實現,但是我的標題對齊已經結束。Android表對齊整齊與凍結標題

繼承人的XML

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/table_header" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" > 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <TableLayout 
      android:id="@+id/table_body" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" > 
     </TableLayout> 
    </ScrollView> 
</TableLayout> 

和繼承人

public class PrepositionRulesActivity extends AppCompatActivity { 

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

    public void init(){ 
     createTableHeader(); 
     createTableBody(); 
     // alignRows(); 
    } 

    private Collection<Preposition> load(Context context) { 
     DataSource<Preposition> dataSource = new DataSourceFactory().createPrepositionDataSource(context); 
     return dataSource.get(); 
    } 

    private void createTableHeader() { 
     TableLayout tableLayout = (TableLayout) findViewById(R.id.table_header); 
     tableLayout.addView(createHeaderRow(), 0); 
    } 

    private void createTableBody() { 
     TableLayout tableLayout = (TableLayout) findViewById(R.id.table_body); 

     int row = 0; 
     TableRow tableRow = createHeaderRow(); 
     tableLayout.addView(tableRow, row++); 
     for (Preposition preposition : load(this)) { 
      tableLayout.addView(createBodyRow(preposition), row++); 
     } 
    } 

    private TableRow createHeaderRow() { 
     TableRow row = new TableRow(this); 
     TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     rowLayout.gravity = Gravity.CENTER_HORIZONTAL; 
     row.setLayoutParams(rowLayout); 
     row.addView(createCell(getString(R.string.preposition))); 
     row.addView(createCell(Kasus.Akkusativ.name())); 
     row.addView(createCell(Kasus.Dativ.name())); 
     row.addView(createCell(Kasus.Genitiv.name())); 
     return row; 
    } 

    private TableRow createBodyRow(Preposition preposition) { 
     TableRow row = new TableRow(this); 
     TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     rowLayout.gravity = Gravity.CENTER_HORIZONTAL; 
     row.setLayoutParams(rowLayout); 
     row.addView(createCell(preposition.getPreposition())); 
     row.addView(createCell(preposition.contains(Kasus.Akkusativ))); 
     row.addView(createCell(preposition.contains(Kasus.Dativ))); 
     row.addView(createCell(preposition.contains(Kasus.Genitiv))); 
     return row; 
    } 

    private void alignRows() { 
     TableLayout headerLayout = (TableLayout) findViewById(R.id.table_header); 
     TableRow headerRow = (TableRow)headerLayout.getChildAt(0); 

     TableLayout bodyLayout = (TableLayout) findViewById(R.id.table_body); 
     TableRow bodyRow = (TableRow)bodyLayout.getChildAt(0); 
     headerLayout.setLayoutParams(bodyLayout.getLayoutParams()); 

     for(int i = 0; i < bodyRow.getChildCount(); i++) { 
      headerRow.getChildAt(i).setLayoutParams(bodyRow.getChildAt(i).getLayoutParams()); 
     } 
    } 

    private View createCell(boolean isEnabled) { 
     if (isEnabled) { 
      ImageView view = new ImageView(this); 
      view.setImageResource(R.drawable.ic_done_black_24dp); 
      return view; 
     } 
     return new Space(this); 
    } 

    private View createCell(String text) { 
     TextView view = new TextView(this); 
     view.setPadding(15, 0, 15, 5); 
     view.setTypeface(Typeface.DEFAULT_BOLD); 
     view.setText(text); 
     return view; 
    } 
} 

我想我可以實現取向通過添加標題行的副本到體表,比複製其佈局的代碼該行添加到標題表,但沒有奏效。

任何建議,將不勝感激

圖像顯示未對準:

[Image showing misalignment]

回答

0

這將是很難控制在tablelayout對準,因爲一列的寬度將始終依賴和將受到同一表中另一行的另一列寬度的影響。

爲了創建一個整齊的對齊表,我建議你使用LinearLayout(用於標題和行)和listview來顯示項目(行)。

例row.xml兩個表的頁眉和列表的列:

<LinearLayout 
    android:id="@+id/row" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:divider="?android:attr/dividerHorizontal" 
    android:showDividers="beginning|middle|end"> 
    <TextView 
     android:id="@+id/model" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:padding="4dp" 
     android:text="@string/model" 
     android:textStyle="bold"/> 
    <TextView 
     android:id="@+id/color" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="4dp" 
     android:text="@string/color" 
     android:textStyle="bold"/> 
    <TextView 
     android:id="@+id/status" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="4dp" 
     android:text="@string/status" 
     android:textStyle="bold"/> 
</LinearLayout> 

這是你如何運用它在你的activity_main.xml中:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="?android:attr/dividerHorizontal"/> 

    <include layout="@layout/row"/> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="?android:attr/dividerHorizontal"/> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</LinearLayout> 

最後,您膨脹同一行。 xml爲列表視圖的適配器中的每個列表項目。下面是上述xml創建的表格的屏幕截圖。請注意,我已經刪除了分隔線,並在顯示錶格中的每個行項目時以編程方式將textstyle設置爲正常。

enter image description here

+0

謝謝,讓我走上正軌 – wboris