繁体   English   中英

android中使用Java代码绘制边框宽度和边框颜色?

[英]border-width and border color using java code in 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>

使用形状。 例如:

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

使用可绘制res中的此文本创建xml,然后在TextView中将其设置为背景。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM