2017-02-06 4 views
0

私はXMLとドラッグを使用し、これは私がEditTextにJavaでボーダーを追加するにはどうすればよいですか?

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> 
私が試してみました何

、それ持っているものである をドロップする代わりに、Javaを使用したコードのレイアウトにしようとしていますdoes not work

 username.setBackground(R.drawable.border); 
     username.setBackgroundResource(R.drawable.border); 
+0

変化ストローク色として設定し、白色で固体加えます。 –

答えて

1

それは単純で、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);

関連する問題