如何使用Java在EditText上添加边框?
我试图用Java编写布局代码,而不是使用XML和拖放 这就是我所拥有的如何使用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
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>