android中使用java代码的边框宽度和边框颜色?

时间:2014-11-11 10:56:47

标签: java android

我想在android中使用Java代码在编辑文本中设置border-width和border-color,我没有使用任何xml文件进行设计,请帮助我成为android开发中的新蜜蜂

这是活动类

public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button myButton = new Button(this);
    myButton.setText("ButtonBox4");
    myButton.setId(1);
    //myButton.setBackgroundColor(Color.YELLOW);
    RelativeLayout myLayout = new RelativeLayout(this);
    Resources r4 = getResources();
    int px4 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 300, r4.getDisplayMetrics());

    myLayout.setMinimumHeight(px4);

    Resources r5 = getResources();
    int px5 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 1022, r5.getDisplayMetrics());

    myLayout.setMinimumWidth(px5);

    RelativeLayout.LayoutParams CheckParams = 
            new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);

    CheckBox checkBox = new CheckBox(this);
    checkBox.setOnCheckedChangeListener(this);
    checkBox.setId(3);
    checkBox.setText("calender6");
    CheckParams.addRule(RelativeLayout.ABOVE, myButton.getId());
    CheckParams.addRule(RelativeLayout.CENTER_VERTICAL);
    CheckParams.setMargins(656, 169, 0, 0);

    Resources r6 = getResources();
    int px6 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 40, r6.getDisplayMetrics());

    checkBox.setHeight(px6);

    Resources r7 = getResources();
    int px7 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 120, r7.getDisplayMetrics());

    checkBox.setWidth(px7);

    EditText myEditText = new EditText(this);
    myEditText.setHint("TextBox2");
    myEditText.setId(2);
    myEditText.setBackgroundColor(Color.rgb(102, 255, 255));

    RelativeLayout.LayoutParams textParams = 
            new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,   
                RelativeLayout.LayoutParams.WRAP_CONTENT);

    textParams.addRule(RelativeLayout.ABOVE, myButton.getId());
    textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    textParams.setMargins(300, 69,0 ,0);

    Resources r = getResources();
    int px = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 200, r.getDisplayMetrics());

    myEditText.setWidth(px);

    Resources r1 = getResources();
    int px1 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 40, r1.getDisplayMetrics());

    myEditText.setHeight(px1);

    Resources r2 = getResources();
    int px2 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 200, r2.getDisplayMetrics());

    myButton.setWidth(px2);

    Resources r3 = getResources();
    int px3 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 40, r3.getDisplayMetrics());

    myButton.setHeight(px3);

    RelativeLayout.LayoutParams buttonParams = 
            new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
    buttonParams.setMargins(437, 199, 0, 0);

    myLayout.addView(myButton, buttonParams);
    myLayout.addView(myEditText, textParams);
    myLayout.addView(checkBox, CheckParams);  
    setContentView(myLayout);
}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynamicviews"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


</manifest>

1 个答案:

答案 0 :(得分:1)

使用形状。例如:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#4fa5d5"/>
</shape>

在drawable res中创建带有此文本的xml,然后在TextView中将其设置为background。

这是您的边框宽度:android:width="1dip"