【问题标题】:Adding Margins to a dynamic Android TextView向动态 Android TextView 添加边距
【发布时间】:2012-07-30 06:24:54
【问题描述】:

我已经查看了关于此问题的大量先前答案,并尝试了标记为工作的代码,但到目前为止,我所做的任何事情都无法说服我在 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();

如前所述,我尝试过的其他解决方案似乎都无法说服 textview 填充所有水平空间。

我已经尝试删除该形状等...

有什么想法吗?

【问题讨论】:

    标签: android textview margin android-relativelayout toast


    【解决方案1】:

    这是将 textview ( text ) 设置为给定边距的简单代码,

    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 为参数

    【讨论】:

      【解决方案2】:

      您将需要包含您的 TextView 的持有者 RelativeLayout

      然后您可以将持有人的重力设置为 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);
      

      【讨论】:

      • Padding 不能解决这个问题,因为它会被应用到 TextView 内部的 TEXT 上,从而增加文本和边框之间的空间。人们希望完整的 TextView 居中或作为给定值从屏幕边框移动
      • 感谢您接受我的评论 +vely。不过,问题仍然存在,因为填充将应用于 TextView 中的文本。你明白我在说什么吗?因为填充是应用于TextView,它的文本将被填充。
      • 对不起,我应该提到它是我需要应用于 textview 的边距,它在它和布局容器之间创建空白。
      • 你可以使用 setBackground(Color.ANY_One) 到你的 textView
      • 这非常接近正确。将 LinearLayout 参数更改为 LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT 然后将 Gravity 设置为 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);为我解决了这个问题。在代码中创建相对布局并向其中添加 Textview 的逻辑在 Toast 试图消失时导致异常。
      猜你喜欢
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多