将边距添加到动态Android TextView

时间:2012-08-01 11:01:56

标签: android textview margin relativelayout toast

我已经查看了一堆以前的答案,并尝试了标记为工作的代码,但到目前为止我所做的一切都无法说服我在runtme创建的TextView从左到右完全填充屏幕。我想在两侧添加一个类似于常规Toast的边距。之后,我可以在形状上添加阴影。

这是我的表单布局。

<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();

如上所述,我从其他解决方案中尝试过的任何东西似乎都说服文本视图填满了所有的水平空间。

我试过去掉那个形状......

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

需要持有人RelativeLayout,它将包含你的TextView

然后您可以将持有者的重力设置为CENTER

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);

答案 1 :(得分:-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的Object作为参数