How to make View stop grow at parent's height?
This is the code. The reference point is parent's height. The text should grow until it reaches its parent's height. However the element keeps growing beyond visible area.
There is no need to post XML file as there is only one button and one textview.
Anyone knows why the text keeps growing beyond the parent's height? What am I doing wrong?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Display display = getWindowManager().getDefaultDisplay();
final int screenWidth = display.getWidth();
final int screenHeight = display.getHeight();
textView = (TextView) findViewById(R.id.textview);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int dTextWidth = textView.getWidth();
if (dTextWidth <= screenHeight) {
textView.setTextSize(textView.getTextSize() + 1);
}
}
});
}
Thanks
EDIT
I used LinearLayout
's height and it still does not work. Even more, the LinearLayout
height is 0 !!!!!
LinearLayout lin = (LinearLayout) findViewById(R.id.linear_layout);
final int layoutHeight = lin.getHeight();
Toast.makeText(this,"LinLay height: "+layoutHeight,Toast.LENGTH_SHORT).show();
...
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int displayTextWidth = textView.getWidth();
if (displayTextWidth <= layoutHeight) {
textView.setTextSize(textView.getTextSize() + 1);
}
开发者_如何学编程}
});
You are comparing the text width with the screen height. It's a simple mistake. Change it to this.
if (dTextWidth <= screenWidth) {
textView.setTextSize(textView.getTextSize() + 1);
}
精彩评论