2012-08-01 66 views
1

我已經查看了一堆以前的答案,並嘗試了標記爲工作的代碼,但到目前爲止,我做的任何事都不能說服我在創建時從填充從左到右完全屏幕。我想在兩邊添加類似於普通Toast的保證金。之後,我可以爲形狀添加陰影。爲一個動態的Android TextView添加頁邊距

這裏我的表單佈局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/rel_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center_vertical|center_horizontal" 
     android:text="@string/hello_world" 
     android:textColor="@color/holobrightblue" 
     android:textSize="48sp" 
     android:textStyle="bold" 
     tools:context=".MainActivity" /> 

</RelativeLayout> 

..和我的代碼

textView = new TextView(m_Context); 
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null); 

ShapeDrawable sd = new ShapeDrawable(rs); 
sd.setAlpha(m_opacity); 
textView.setBackgroundDrawable(sd);    
textView.setTextColor(m_txtcolor); 
textView.setText(toasttitle+"\n"+toastmessage); 
textView.setBackgroundColor(Color.BLACK); 
textView.setPadding(10,10,10,10); 

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); 


lp.setMargins(60, 0, 60, 0);   
textView.setLayoutParams(lp); 

toastView = new Toast(m_Context); 
toastView.setView(textView); 
toastView.setDuration(m_toastlen); 
toastView.setGravity(m_screengravity, 0,0); 
toastView.show(); 

如前所述,沒有什麼我已經從其他的解決方案試圖似乎填滿所有的臥式空間說服TextView的。

我試過去除那個形狀等...

任何想法?

回答

3

您將需要持有人的RelativeLayout其中將包含您的TextView

,然後你可以將持有者的比重設定爲中央

RelativeLayout head2 = new RelativeLayout(this); 
head2.setId(++myid); 
RelativeLayout.LayoutParams head2Params = new RelativeLayout.LayoutParams(
     LayoutParams.FILL_PARENT, 25); 
head2Params.addRule(RelativeLayout.BELOW, head1.getId()); 
head2.setLayoutParams(head2Params); 
head2.setBackgroundColor(Color.CYAN); 

那麼你創建的TextView

textView = new TextView(m_Context); 
RoundRectShape rs = new RoundRectShape(new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }, null, null); 

ShapeDrawable sd = new ShapeDrawable(rs); 
sd.setAlpha(m_opacity); 
textView.setBackgroundDrawable(sd);    
textView.setTextColor(m_txtcolor); 
textView.setText(toasttitle+"\n"+toastmessage); 
textView.setBackgroundColor(Color.BLACK); 
textView.setPadding(10,10,10,10); 

LinearLayout.LayoutParams lp = new 
    LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
              ^^^^^^^^^^^^ 
              ^^^^^^^^^^^^MUST EDIT 
lp.setMargins(60, 0, 60, 0);   
textView.setLayoutParams(lp); 

那麼你的TextView與重心添加到持有人的RelativeLayout

head2.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 
head2.addView(textView); 

顯示它

toastView = new Toast(m_Context); 
toastView.setView(textView); 
toastView.setDuration(m_toastlen); 
toastView.setGravity(m_screengravity, 0,0); 
+0

填充不會解決這個問題,因爲它會被應用於文本TextView的內部,使文字和邊框之間的空間將得到increased.The人想要完整TextView以居中或移動爲屏幕邊框的給定值 – 2012-08-01 11:18:44

+0

請參閱我編輯的答案 – 2012-08-01 11:19:10

+0

感謝您接受我的評論+ vely.Still,問題將保留,因爲填充將應用於TextView中的文本。你知道我在說什麼?因爲填充是應用於TextView的,所以文本會被填充。 – 2012-08-01 11:22:35

-1

這是最簡單的代碼來設置的TextView(文本)給定的空間,

LayoutParams lp = new LayoutParams(text.getWidth(), text.getHeight()); 
     lp.setMargins(5, 5, 0, 5); 
     text.setLayoutParams(lp); 

setMargins()的參數如下:

LayoutParams.setMargins(int left,int top , int right,int bottom) 

setLayoutParams採取的LayoutParams的對象作爲參數