[英]How do i add a border on EditText with Java?
我正在尝试使用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
<!-- Background Color -->
<!-- Border Color -->
<stroke android:width="3dp" android:color="#ffffff" />
<!-- Round Corners -->
<corners android:radius="15dp" />
</shape>
我尝试过的方法不起作用
username.setBackground(R.drawable.border);
username.setBackgroundResource(R.drawable.border);
很简单,您需要在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.setbackground(R.drawable.border);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.