【问题标题】:setting padding for textview not working为 textview 设置填充不起作用
【发布时间】:2017-01-25 21:49:30
【问题描述】:

我正在尝试通过 textview 实现标签。我在 EditText 中输入一些内容,然后按一个按钮来添加标签。 我以编程方式向 textview 添加填充和矩形背景,但填充设置没有任何效果。如何添加填充以使其正常工作?

public void tag_it_callback (View view) {
    Log.v("actv", "tag it callback");
    LinearLayout main_layout = (LinearLayout) findViewById(R.id.main_layout);

    TextView tv = new TextView(this);

    tv.setText( ((EditText) findViewById(R.id.textfield)).getText() );
    main_layout.addView(tv);

    transform_tag(tv);
}

public void transform_tag(TextView tv) {

    // set padding and background
    int padding = 10;
    tv.setPaddingRelative(20, 20, 20, 20);
    tv.setPadding(20, 20, 20, 20);
    tv.setBackgroundResource(R.drawable.border);

    // set layout width and height
    LayoutParams params= (LayoutParams) tv.getLayoutParams();
    Log.v("actv", "params "+params);
    if ( params != null ) {
        params.width=LayoutParams.WRAP_CONTENT;
        params.height=LayoutParams.WRAP_CONTENT;
    } else {
        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
    tv.setLayoutParams(params)
}

border.xml 是这样的

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item> 
         <shape android:shape="rectangle">
      <solid android:color="#FF0000" /> 
      <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />
    </shape>
  </item>   
  <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">  
    <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
     </layer-list>"`<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item> 
    <shape android:shape="rectangle">
      <solid android:color="#FF0000" /> 
      <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />
    </shape>
  </item>   
  <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">  
    <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
     </layer-list>`

【问题讨论】:

    标签: android textview padding


    【解决方案1】:

    我终于找到了问题 在设置填充之前,我必须先设置背景。 设置填充然后设置背景不起作用

    // does not work
    tv.setPadding(20, 20, 20, 20);
    tv.setBackgroundResource(R.drawable.border);
    
    
    // works
    tv.setBackgroundResource(R.drawable.border);
    tv.setPadding(20, 20, 20, 20);
    

    【讨论】:

    • 这已在 Android KitKat 上修复(可能在早期版本中)
    • 谢谢!这让我难过一阵子,因为我的填充没有被应用到 Android 4.3 上。原来是因为我在设置填充后设置了视图的颜色。
    • 我在 Nougat 模拟器中遇到过这个错误。很奇怪。
    • 我在装有 Android 4.2.2 的三星 Galaxy S3 上进行了类似的测试。即使已经在 xml 布局中设置了填充,但后来以编程方式设置背景时,填充被忽略了。我最终通过仅以编程方式设置背景和填充并按照此答案中的建议以正确的顺序进行修复。
    【解决方案2】:
    tv.setPadding(20, 20, 20, 20);
    tv.setBackgroundResource(R.drawable.border);
    

    【讨论】:

    • 那行不通。我没有看到填充。背景边框还是离文字很近。
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2013-12-18
    • 2015-01-27
    • 2013-06-04
    • 2021-09-29
    • 2016-04-24
    • 2018-01-19
    相关资源
    最近更新 更多