2012-08-01 75 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的对象作为参数