添加边距为动态的Andr​​oid的TextView [英] Adding Margins to a dynamic Android TextView

查看:135
本文介绍了添加边距为动态的Andr​​oid的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过这个previous答案一堆看了看,并试图在code标记为工作,但到目前为止,我没有什么可以做填满屏幕说服我创建在runtme TextView的完全从左到右。我想补充两侧类似于普通面包的余量。在那之后我可以添加阴影的形状。

I've looked through a heap of previous answers on this , and tried the code marked as working, but so far nothing I do can convince the TextView I'm creating at runtme from filling the screen completely from left to right. I want to add a margin on both sides similar to a regular Toast. After that I can add a drop shadow to the shape.

下面我的窗体布局。

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

..和我的code

.. and my code

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的。

As mentioned, nothing I've tried from other solutions seems to convince the textview from filling all the horizonal space.

我试过去除形状等等...

I've tried removing that shape etc...

任何想法?

推荐答案

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

这篇关于添加边距为动态的Andr​​oid的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆