2

我试图弄清楚如何制作一个自定义的 EditText,它的左右两侧都有黑色边框,顶部有绿色边框,底部有蓝色边框。见下文: 在此处输入图像描述

我对 Android 开发相当陌生,我花了很多时间阅读他们的文档,但没有找到任何关于这种定制的任何东西。我知道在 CSS 中你可以只使用border-right、border-left等属性,但不确定它在Android开发中是否那么简单。我正在寻找最兼容的解决方案,最好是 2.3 版(姜饼)。

4

3 回答 3

1

您必须制作自定义图像以用作背景。它相对简单,您需要使用2D 图形指南中描述的 9-patch 。

一旦你有了它,你将把它放在你项目的 res/drawable 文件夹中,然后将它与 XML 中的 EditText 一起使用

<EditText
    android:background="@drawable/my_custom_background"
    ...
/>
于 2012-12-05T18:22:18.273 回答
0

创建一个LayerList带有所需渐变颜色的正方形,并在其上方创建一个带有一些边框的白色正方形。然后使用这个drawable作为你的TextView背景。

于 2012-12-05T18:26:00.803 回答
0

您可以使用 9-patch 或在 xml 中<layer-list> 创建多色边框 EditText,我正在以编程方式创建多色边框 EditText。

public class MainActivity extends AppCompatActivity {
 private TextView mTextView;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      customTextViewWithBorder();

}

private void customTextViewWithBorder(){
mTextView = (TextView) findViewById(R.id.tv);
         // Initialize some new ColorDrawable objects
                        ColorDrawable leftBorder = new ColorDrawable(Color.RED);
                        ColorDrawable topBorder = new ColorDrawable(Color.GREEN);
                        ColorDrawable rightBorder = new ColorDrawable(Color.BLUE);
                        ColorDrawable bottomBorder = new ColorDrawable(Color.YELLOW);
                        ColorDrawable background = new ColorDrawable(Color.WHITE);

                        // Initialize an array of Drawable objects
                        Drawable[] layers = new Drawable[]{
                                leftBorder, // Red color
                                topBorder, // Green color
                                rightBorder, // Blue color
                                bottomBorder, // Yellow color
                                background // White background
                        };
     // Initialize a new LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(layers);
    // Red layer padding, draw left border
                    layerDrawable.setLayerInset(0,0,0,15,0);

                    // Green layer padding, draw top border
                    layerDrawable.setLayerInset(1,15,0,0,15);

                    // Blue layer padding, draw right border
                    layerDrawable.setLayerInset(2,15,15,0,0);

                    // Yellow layer padding, draw bottom border
                    layerDrawable.setLayerInset(3,15,15,15,0);

                    // White layer, draw the background
                    layerDrawable.setLayerInset(4,15,15,15,15);
     mTextView.setBackground(layerDrawable);

                    // Set the TextView padding
                    mTextView.setPadding(25,25,25,25);

}

源码安卓——代码

于 2016-06-25T11:14:16.693 回答