【问题标题】:Adding divider in TableLayout在 TableLayout 中添加分隔线
【发布时间】:2018-12-30 13:20:04
【问题描述】:

我正在尝试为动态创建的表列设置分隔符。我的 TableLayout xml:

<TableLayout
    android:id="@+id/tableEdit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/table_header_divider"/>

我动态添加列的部分:

TableRow headerrow = new TableRow(this.getActivity());
addTableHeader("Type", 175, headerrow);
addTableHeader("Exp", 175, headerrow);
headerrow.setBackground(ContextCompat.getDrawable(this.getActivity(), R.drawable.selector_table_header));
headerrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tableEdit.addView(headerrow);

private void addTableHeader(String title, int colWidth, TableRow headerrow) {
   TextView textview = new TextView(this.getActivity());
   textview.setText(title);
   textview.setLayoutParams(new android.widget.TableRow.LayoutParams(colWidth, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
   headerrow.addView(textview);
}

但是,分隔线图像根本没有显示。有任何想法吗?或者有没有办法以编程方式设置分隔符?谢谢!

【问题讨论】:

标签: java android tablelayout android-tablelayout


【解决方案1】:

试试这个

private void addTableHeader(String title, int colWidth, TableRow headerrow) {

    TextView textview = new TextView(this);
    textview.setText(title);
    textview.setLayoutParams(new android.widget.TableRow.LayoutParams(colWidth, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
    headerrow.addView(textview);

    View v = new View(this);
    v.setLayoutParams(new TableRow.LayoutParams(3, TableRow.LayoutParams.MATCH_PARENT));
    v.setBackgroundColor(Color.RED);
    headerrow.addView(v);
}

编辑您可以添加一个布尔标志来检查您是否需要添加分隔符的天气

public class MyActivity extends AppCompatActivity {


    TableLayout tableEdit;

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

        tableEdit = findViewById(R.id.tableEdit);

        TableRow headerrow = new TableRow(this);
        addTableHeader("Type", 175, headerrow, true);// send true if you want to add devider
        addTableHeader("Exp", 175, headerrow, false);// send false if you don't want to add devider
        headerrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

        tableEdit.addView(headerrow);


    }

    private void addTableHeader(String title, int colWidth, TableRow headerrow, boolean flag) {

        TextView textview = new TextView(this);
        textview.setText(title);
        textview.setLayoutParams(new android.widget.TableRow.LayoutParams(colWidth, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
        headerrow.addView(textview);

        if (flag) {
            View v = new View(this);
            v.setLayoutParams(new TableRow.LayoutParams(3, TableRow.LayoutParams.MATCH_PARENT));
            v.setBackgroundColor(Color.RED);

             ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        params.leftMargin = 100;
        params.rightMargin = 200;

        v.setLayoutParams(params);
            headerrow.addView(v);

        }
    }

}

【讨论】:

  • 非常感谢!但是根据问题中的屏幕截图,分隔线太靠近下一列。我试图添加 paddingRight 但它根本没有改变。有什么想法吗?
  • @hyperfkcb 对不起,我不明白,如果您有新问题,请发布更多详细信息,以便其他用户可以尝试帮助您,我的朋友
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 2019-10-24
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多