android - 为 textview 设置填充不起作用

标签 android textview padding

我正在尝试通过 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>`

最佳答案

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

// 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 - 为 textview 设置填充不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18327498/

相关文章:

android - 使用 Ant 构建的 Android 项目中的库资源

Android 处理程序仅使用最后一个 setText() 更新 TextView

java - Fragment Intermediate(I) :Getting input from edittext, 在 fragment 的 TextView 中设置文本

java - 通过按按钮使 Textview 可见

html - 如何使 html 填充在不同浏览器中看起来相同?

android - 错误 : INTERNAL_SERVICE_ERROR in Paypal Android SDK

android - 应用程序未运行

android - Genymotion 应用程序崩溃/卡住

Java AWT 组件和面板填充/边框

reactjs - 删除可堆叠 Grid.Row 中的填充