【问题标题】:Setting gravity for auto generated TextView and Button为自动生成的 TextView 和 Button 设置重力
【发布时间】:2012-05-04 19:34:13
【问题描述】:

如何设置gravitymargin 用于自动生成(在代码中)TextViewsButtons?它们位于LinearLayout 内。

这是我的生成代码:

        for(int i=0; i<num_enter; i++){
    final int cuco = i;
    LinearLayout linlay = new LinearLayout(this);
    linlay.setOrientation(0);
    TextView text = new TextView(this);
    text.setText(name[cuco] + "        ");
    linlay.addView(text);
    TextView num = new TextView(this);
    num.setId(cuco);
    num.setText("" + current[cuco]);
    linlay.addView(num);
    Button minus = new Button(this);
    minus.setText("-");
    minus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int cval = current[cuco];
            int currentF = current[cuco] - 1;
            current[cuco] = current[cuco] -1;
            SetSql update = new SetSql(SpellCast.this);
            update.open();
            update.changeCurrent(currentF, cval, name[cuco]);
            update.close();
            ((TextView) findViewById(cuco)).setText("" + currentF);
        }
    });
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
    minus.setLayoutParams(p);
    linlay.addView(minus);
    Button plus = new Button(this);
    plus.setText("+");
    plus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int cval = current[cuco];
            int currentF = current[cuco] + 1;
            current[cuco] = current[cuco] + 1;
            SetSql update = new SetSql(SpellCast.this);
            update.open();
            update.changeCurrent(currentF, cval, name[cuco]);
            update.close();
            ((TextView) findViewById(cuco)).setText("" + currentF);
        }
    });
    LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
    plus.setHeight(LayoutParams.WRAP_CONTENT);
    plus.setWidth(50);
    plus.setLayoutParams(w);
    linlay.addView(plus);

    main.addView(linlay);
    }

希望你能找到一种方法来设置按钮的重力而不改变它的大小。

【问题讨论】:

  • 很多人都在尝试提供帮助......但我们都只是猜测,直到您发布一些代码。我知道代码对你来说似乎很明显......但你是说所有给定的答案都不起作用,即使他们完美地回答了你的问题
  • 这与你想要的结果是什么?
  • 这样的结果是按钮和文本字段都粘在了左边

标签: android android-layout margin gravity


【解决方案1】:

1) 获取 TextView say tv 的引用。

2) tv.setGravity(Gravity.CENTER);

不确定是否适用于 LinearLayout.. 尝试相对或绝对

【讨论】:

  • 这将只设置文本的重力,而不是 TextView
【解决方案2】:

使用View.setLayoutParams(ViewGroup.LayoutParams params)。查看http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout 中的LayoutParams

【讨论】:

  • 对不起,但就像你下面的答案一样,它不起作用。还有另一种方法吗?我正在使用很多按钮和文本视图,并且在在线使用此方法时给予左边距使它们彼此站立......
  • 你能发布一个你希望它们如何布局的例子吗?
  • 我不能发布任何示例,我想要的只是一种从左侧边距自动生成的按钮和文本视图的方法
【解决方案3】:

由于没有人解决边距问题

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;

textView.setLayoutParams(p);

使用 setLayoutParams 时,请确保布局参数的类型与父视图相匹配,在本例中为 LinearLayout

setGravity(Gravity.CENTER); 将设置文本在视图中的重力

【讨论】:

  • 这个想法听起来不错,但实际上它行不通。它只是使所有按钮相互粘连,因此没有人被看到。没有其他简单的解决方案吗?
  • 好吧,我不知道你所说的“相互依存”是什么意思。如果您发布了一些有关如何添加按钮/文本视图的代码,我们可能会提供更多帮助:)
  • 好吧,真的没什么可发的。我的意思是按钮不断形成,就像一个在另一个之上。
  • 如果您使用的是线性布局,那应该是不可能的。使用线性布局时,视图不应该像那样重叠。不过,如果您使用的是 RelativeLayout,那么它是有意义的
  • 您是否在线性布局中正确设置了方向?您是否尝试将 TextViews / Buttons 的宽度更改为 wrap_content 而不是 match_parent 像我一样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 1970-01-01
  • 2016-03-20
  • 2013-08-13
  • 2011-06-16
相关资源
最近更新 更多