我如何以编程方式添加余量的TextView甚至为的TableRow基于android应用程序? [英] how do i add margin programmatically for TextView or even TableRow for android-based app?

查看:172
本文介绍了我如何以编程方式添加余量的TextView甚至为的TableRow基于android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经试过增加保证金编程

我已经尝试使用

  TextView的电视=新的TextView(本);
的LayoutParams PARAMS =新的LayoutParams(WRAP_CONTENT,WR​​AP_CONTENT);
params.setMargins(100,20,0,0);
layout.addView(TV,则params);

它使用android.widget.LinearLayout.LayoutParams,但因为表,我得到了运行时错误吧。

我应该怎么做呢?

这是我的code:

  //创建表行
TRA的TableRow =新的TableRow(本);
        trA.setId(1000 + I);
        trA.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));        //将数据添加到表行
        TextView的TVA =新的TextView(本);
        tvA.setId(2000 + I);
        tvA.setText(aList.get(ⅰ)的ToString());
        tvA.setTextSize(18);
        tvA.setTextColor(Color.BLACK);
        tvA.setClickable(真);
        tvA.setOnClickListener(本);
        tvA.setSingleLine(假);
        tvA.setHorizo​​ntallyScrolling(假);
        tvA.setWidth(0);
        tvA.setHeight(100);        trA.addView(TVA);
        tableA.addView(TRA,新TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

==与进口android.widget.TableLayout.LayoutParams编辑; ==

我添加了这个在那里我宣布tablerow的结束

 的LayoutParams PARAMS =新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0,0,0,20);

和编辑的addView在表A

  tableA.addView(TVA,则params);

我在日志猫错误,显示相当了很多,所以我给了主要的一个位置:指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。


解决方案

  TableRow.LayoutParams PARAMS =新TableRow.LayoutParams(WRAP_CONTENT,WR​​AP_CONTENT);
params.setMargins(100,20,0,0);

hi i've tried to add margin programmatically

i've tried using

TextView tv = new TextView(this);
LayoutParams params = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
params.setMargins(100, 20, 0, 0);
layout.addView(tv, params);

which uses android.widget.LinearLayout.LayoutParams, but because of the table, i got runtime error for it.

how should i do it then ?

this is my code :

//create table row
TableRow trA = new TableRow(this);
        trA.setId(1000+i);
        trA.setLayoutParams(new LayoutParams (LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

        //add data to table row
        TextView tvA = new TextView (this);
        tvA.setId(2000+i);
        tvA.setText(aList.get(i).toString());
        tvA.setTextSize(18);
        tvA.setTextColor(Color.BLACK);
        tvA.setClickable(true);
        tvA.setOnClickListener(this);
        tvA.setSingleLine(false);
        tvA.setHorizontallyScrolling(false);
        tvA.setWidth(0);
        tvA.setHeight(100);

        trA.addView(tvA);
        tableA.addView(trA,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

== edited with import android.widget.TableLayout.LayoutParams; ==

i added this at the end where i declare the tablerow

LayoutParams params = new LayoutParams (LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 20);

and edited the addView at tableA

tableA.addView(tvA, params);

i have error in the log cat, it display quite alot, so i gave the main one here : The specified child already has a parent. You must call removeView() on the child's parent first.

解决方案

TableRow.LayoutParams params = new TableRow.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
params.setMargins(100, 20, 0, 0);

这篇关于我如何以编程方式添加余量的TextView甚至为的TableRow基于android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆