2014-05-04 93 views
0

我正在創建一個TextView併爲其設置LayoutParams。 但是,當我顯示TextView的大小爲0設置TextView的寬度

下面是我的代碼:

TextView t = new TextView(getBaseContext()); 
t.setBackgroundResource(R.drawable.roundbox); 
t.setText(new String("Hi").toCharArray(), 0, 2); 

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height); 
lp.bottomMargin = 10; 
lp.gravity = Gravity.RIGHT; 
t.setLayoutParams(lp); 

的roundbox.xml內容是:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
     <corners android:radius="8dp"/> 
    <solid android:color="#FFFFFF"/> 
    <stroke android:width="1dp" android:color="#4fa5d5"/> 
</shape> 

能否請你讓我知道什麼應該我在LayoutParams構造函數中設置寬度和高度。我設置了一些任意值,但是我希望TextView的大小基於文本的大小。

回答

1

要設置文本數量的寬度,您可以在寬度參數中設置LinearLayout.LayoutParams.WRAP_CONTENT
它將根據內容大小設置高度或寬度。
請參閱documentation,您可以在其中找到恆定值以根據需要設置高度和寬度。

相關問題