Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
如何使用Java在EditText上添加边框?_Java_Android_Xml - Fatal编程技术网

如何使用Java在EditText上添加边框?

如何使用Java在EditText上添加边框?,java,android,xml,Java,Android,Xml,我试图用Java编写布局代码,而不是使用XML和拖放 这就是我所拥有的 RelativeLayout relativeLayout = new RelativeLayout(this); EditText username = new EditText(this); Button mybutton = new Button(this); mybutton.setId(1); username.setId(2); RelativeLayout.Lay

我试图用Java编写布局代码,而不是使用XML和拖放 这就是我所拥有的

  RelativeLayout relativeLayout = new RelativeLayout(this);
    EditText username = new EditText(this);
    Button mybutton = new Button(this);

    mybutton.setId(1);
    username.setId(2);

    RelativeLayout.LayoutParams usernameDetails =
            new

                    RelativeLayout.LayoutParams(

                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
    usernameDetails.addRule(RelativeLayout.ABOVE, mybutton.getId());
    usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
    usernameDetails.addRule(RelativeLayout.CENTER_VERTICAL);

    //adding margins between widgets
    usernameDetails.setMargins(0,0,0,50);
我的边界XML


很简单,您需要在drawable文件夹中创建border.xml:

 <?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="2dp"
        />
    <solid android:color="#ffffff"
        />
    <stroke
        android:width="3dp"
        android:color="#000000" />
</shape>

然后将其设置为editext.setbackgroundR.drawable.border

将笔划颜色更改为除白色以外的其他颜色,并添加带有白色的纯色。
      username.setBackground(R.drawable.border);
      username.setBackgroundResource(R.drawable.border);
 <?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="2dp"
        />
    <solid android:color="#ffffff"
        />
    <stroke
        android:width="3dp"
        android:color="#000000" />
</shape>