Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中使用java代码的边框宽度和边框颜色?_Java_Android - Fatal编程技术网

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

android中使用java代码的边框宽度和边框颜色?,java,android,Java,Android,我想在android中使用Java代码在编辑文本时设置边框宽度和边框颜色,我没有使用任何xml文件进行设计,请帮助我是android开发的新手 这是活动课 public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(

我想在android中使用Java代码在编辑文本时设置边框宽度和边框颜色,我没有使用任何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>

在drawable res中使用此文本创建xml,然后在TextView中将其设置为背景


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

在android中没有边框您应该使用xml代码…布局边界对此很有帮助。您可以为textview设置背景图像。。。我不确定布局边界(9个补丁)请再试一次。。。