Java 如何在Android中定义不同颜色和宽度的EditText
这就是我如何向Java 如何在Android中定义不同颜色和宽度的EditText,java,android,android-edittext,border,android-view,Java,Android,Android Edittext,Border,Android View,这就是我如何向EditText添加边框的方法。如何仅在EditText的三面添加边框,并为每个边框定义不同的颜色和宽度 EditText editText = new EditText(this); editText.setText("Find"); editText.setWidth(555); GradientDrawable border = new GradientDrawable(); border.setColor(0xFFFFFFFF); // white background
EditText
添加边框的方法。如何仅在EditText
的三面添加边框,并为每个边框定义不同的颜色和宽度
EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);
GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF); // white background
border.setStroke(1, 0xFF000000); // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackgroundDrawable(border);
} else {
editText.setBackground(border);
}
EditText EditText=新的EditText(此);
editText.setText(“查找”);
editText.setWidth(555);
GradientDrawable边框=新的GradientDrawable();
border.setColor(0xFFFFFFFF);//白色背景
border.setStroke(1,0xFF000000);//黑边满
if(Build.VERSION.SDK\u INT
我下面的尝试无效:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:right="5dp">
<shape android:shape="rectangle">
<solid android:color="#FFFF" />
</shape>
</item>
<item android:left="22dp">
<shape android:shape="rectangle">
<solid android:color="#746565" />
</shape>
</item>
</layer-list>
Vielen dank im voraus.创建一个可绘制的图层列表,定义3个不同的矩形形状,并自定义宽度和颜色,隐藏每个形状的3个侧面以仅显示一面。像这样:
my_edittext\u border.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list android:paddingLeft="30dp" xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-1dp"
android:left="-1dp"
android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#ff0000" />
</shape>
</item>
<item
android:bottom="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="3dp"
android:color="#11ee66" />
</shape>
</item>
<item
android:bottom="-5dp"
android:left="-5dp"
android:top="-5dp">
<shape android:shape="rectangle">
<stroke
android:width="5dp"
android:color="#0000ff" />
</shape>
</item>
</layer-list>
创建一个可绘制的图层列表,并定义3个不同的矩形形状(具有自定义宽度和颜色),隐藏每个形状的3个侧面以仅显示一面。像这样:
my_edittext\u border.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list android:paddingLeft="30dp" xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-1dp"
android:left="-1dp"
android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#ff0000" />
</shape>
</item>
<item
android:bottom="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="3dp"
android:color="#11ee66" />
</shape>
</item>
<item
android:bottom="-5dp"
android:left="-5dp"
android:top="-5dp">
<shape android:shape="rectangle">
<stroke
android:width="5dp"
android:color="#0000ff" />
</shape>
</item>
</layer-list>
将此可绘制设置为背景,并根据您的选择进行修改:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--Top border-->
<item
android:bottom="-2dp"
android:left="-2dp"
android:right="-2dp"
android:top="0dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/black" />
</shape>
</item>
<!--Bottom border-->
<item
android:bottom="0dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/orange" />
</shape>
</item>
<!--Right border-->
<item
android:bottom="-2dp"
android:left="-2dp"
android:right="0dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/remove" />
</shape>
</item>
<!--Left border-->
<item
android:bottom="-2dp"
android:left="0dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/green" />
</shape>
</item>
</layer-list>
将此可绘制文件设置为背景,并根据您的选择进行修改:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--Top border-->
<item
android:bottom="-2dp"
android:left="-2dp"
android:right="-2dp"
android:top="0dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/black" />
</shape>
</item>
<!--Bottom border-->
<item
android:bottom="0dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/orange" />
</shape>
</item>
<!--Right border-->
<item
android:bottom="-2dp"
android:left="-2dp"
android:right="0dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/remove" />
</shape>
</item>
<!--Left border-->
<item
android:bottom="-2dp"
android:left="0dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/green" />
</shape>
</item>
</layer-list>
首先,您还可以使用vector drawable绘制以下答案:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="200px"
android:width="200px"
android:viewportHeight="150"
android:viewportWidth="200">
<path
android:strokeColor="@android:color/black"
android:strokeWidth="1"
android:pathData="M0,0 L100,0 "/>
<path
android:strokeColor="@android:color/white"
android:strokeWidth="1"
android:pathData="M100,0 L100,100 "/>
<path
android:strokeColor="@android:color/holo_red_light"
android:strokeWidth="1"
android:pathData="M100,100 L0,100 "/>
<path
android:strokeColor="@android:color/holo_green_dark"
android:strokeWidth="1"
android:pathData="M0,100 L0,0 "/>
</vector>
首先,您还可以使用vector drawable绘制以下答案:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="200px"
android:width="200px"
android:viewportHeight="150"
android:viewportWidth="200">
<path
android:strokeColor="@android:color/black"
android:strokeWidth="1"
android:pathData="M0,0 L100,0 "/>
<path
android:strokeColor="@android:color/white"
android:strokeWidth="1"
android:pathData="M100,0 L100,100 "/>
<path
android:strokeColor="@android:color/holo_red_light"
android:strokeWidth="1"
android:pathData="M100,100 L0,100 "/>
<path
android:strokeColor="@android:color/holo_green_dark"
android:strokeWidth="1"
android:pathData="M0,100 L0,0 "/>
</vector>
谢谢@Sisa的回答。如何在左侧添加填充物?我尝试了android:left=“12dp”
,但这只会将左边框进一步向内推进12像素。@Program Me Rev将填充添加到图层列表android:paddingLeft=“30dp”
Hi@SiSa。我试过了,但是没有改变。如果可以的话,你能把它包括在你给出的答案中吗?我是一个完全的Android开发新手。@Programme Me Rev我更改了代码。请注意,如果您在drawable中设置了padding(就像我在上面代码中所做的那样),那么您不能在布局中为视图设置padding,否则drawable padding将被忽略。谢谢@Sisa的回答。如何在左侧添加填充物?我尝试了android:left=“12dp”
,但这只会将左边框进一步向内推进12像素。@Program Me Rev将填充添加到图层列表android:paddingLeft=“30dp”
Hi@SiSa。我试过了,但是没有改变。如果可以的话,你能把它包括在你给出的答案中吗?我是一个完全的Android开发新手。@Programme Me Rev我更改了代码。请注意,如果您在drawable中设置了padding(就像我在上面代码中所做的那样),那么您不能在布局中为视图设置padding,否则drawable padding将被忽略。谢谢@Usman Rana的回答。如何在左侧添加填充物?我尝试了android:left=“12dp”,但这只会将左边框进一步向内推12像素。如果我的答案中也添加了左边框,请向下滚动代码以进行检查。您需要在-ve中提供值以隐藏您想要的边界感谢@Usman Rana为您提供答案。如何在左侧添加填充物?我尝试了android:left=“12dp”,但这只会将左边框进一步向内推12像素。如果我的答案中也添加了左边框,请向下滚动代码以进行检查。您需要在-ve中指定值以隐藏所需的边框