2017-03-10 1 views
1

固定ヘッダーでテーブルを作成しようとしています。私は、別のテーブルレイアウトをラップするスクロールビューを含むテーブルレイアウトを持つことでそれを達成できますが、ヘッダーの配置は外です。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によって作成されたテーブルのスクリーンショットです。テーブル内の各行項目を表示するときは、除算器を削除して、プログラムでテキストスタイルを通常に設定することに注意してください。

enter image description here

+0

ありがとうございました – wboris

関連する問題