【问题标题】:Android: TextView Background Drawable - Allow height of 0?Android:TextView 背景可绘制 - 允许高度为 0?
【发布时间】:2012-05-13 05:22:45
【问题描述】:

我在 Android 中使用 TextView 对象的背景图像,我想知道高度的奇怪行为。

为什么我不能将 TextView 的高度设置为 0,所以它变得“不可见”并且不再占用任何空间 - 设置背景图像时?

这里有一些测试代码演示了 - 对我来说 - 奇怪的行为。

public class HelloAndroid extends Activity implements View.OnClickListener{

TextView btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    btn = new TextView(this);
    btn.setOnClickListener(this);
    //Put in any drawable of you in here
    btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.restablebg5));
    btn.setHeight(80);

    LinearLayout v = new LinearLayout(this);
    v.setOrientation(LinearLayout.VERTICAL);
    v.addView(btn);
    setContentView(v);        

}

public void onClick(View arg0) {
    btn.setHeight(0);

}

}

如何将 textview 的高度设置为 0?

谢谢

【问题讨论】:

    标签: android textview height


    【解决方案1】:

    改为这样做:

    btn.setVisibility(View.GONE);

    您还可以使用: btn.setVisibility(View.INVISIBLE);

    第二个选项将保留空间,但您的项目将不可见 - 第一个选项将您的项目从显示中完全移除(并且不保留空间)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多