文字列的表示区域的表示间距设定方法,CheckBox的父类TextView提供了「setPadding」的方法。
setPadding public void setPadding(int left, int top, int right, int bottom) |
Sets the padding.
Parameters: left the left padding in pixels top the top padding in pixels right the right padding in pixels bottom the bottom padding in pixels
|
具体使用如下所示:
@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle);
CheckBox checkbox = new CheckBox(this); checkbox.setText("checkbox"); checkbox.setPadding(5, 5, 5, 5); setContentView(checkbox); }
|
使用例子 Test05_01.java
package jp.javadrive.android;
import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; import android.view.ViewGroup; import android.widget.TextView; import android.widget.CheckBox; import android.graphics.Color;
public class Test05_01 extends Activity { private final int FP = ViewGroup.LayoutParams.FILL_PARENT; private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
@Override protected void onCreate(Bundle icicle) { super.onCreate(icicle);
LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); setContentView(linearLayout);
TextView text = new TextView(this); text.setText("What would you like to have?"); linearLayout.addView(text, createParam(WC, WC));
CheckBox checkbox1 = new CheckBox(this); checkbox1.setText("Hamburger"); checkbox1.setBackgroundColor(Color.RED); linearLayout.addView(checkbox1, createParam(WC, WC));
CheckBox checkbox2 = new CheckBox(this); checkbox2.setText("Coffee"); checkbox2.setBackgroundColor(Color.GREEN); checkbox2.setPadding(5, 5, 5, 5); linearLayout.addView(checkbox2, createParam(WC, WC));
CheckBox checkbox3 = new CheckBox(this); checkbox3.setText("Potato"); checkbox3.setBackgroundColor(Color.YELLOW); checkbox3.setPadding(30, 5, 5, 5); linearLayout.addView(checkbox3, createParam(WC, WC)); }
private LinearLayout.LayoutParams createParam(int w, int h){ return new LinearLayout.LayoutParams(w, h); } }
|
编译以后运行结果画面

此外还可以用线性布局做两个空间精确调整位置以及使用万能的空格!!!