添加边距为动态的Android的TextView(Adding Margins to a dynam

2019-09-20 14:18发布

我已经通过这个以前的答案一堆看,并试图标记为工作的代码,但到目前为止,我没有什么可以做说服我完全填满屏幕从左至右创建于runtme 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的。

我试过去除形状等..

有任何想法吗?

Answer 1:

您将需要持有人的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);


Answer 2:

这是最简单的代码来设置的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的对象作为参数



文章来源: Adding Margins to a dynamic Android TextView