2

左右に黒い境界線があり、上に緑の境界線があり、下に青い境界線があるカスタム EditText を作成する方法を見つけようとしています。下記参照: ここに画像の説明を入力

私は Android 開発にかなり慣れていないので、ドキュメントを読むのにかなりの時間を費やしましたが、この種のカスタマイズについて何も見つけることができませんでした。CSS では、border-right、border-left などのプロパティを使用できることは知っていますが、Android 開発でそれほど簡単かどうかはわかりません。できればバージョン 2.3 (Gingerbread) から、最も互換性のあるソリューションを探しています。

4

3 に答える 3

1

背景として使用するカスタム イメージを作成する必要があります。比較的簡単です。2D グラフィックス ガイドで説明されているように、9 パッチを使用することをお勧めします。

それを取得したら、それをプロジェクトの res/drawable フォルダーに配置し、XML の EditText で次のように使用します。

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

LayerList必要なグラデーションで色付けされた正方形を作成し、その上にいくつかの境界線を持つ白い正方形を作成します。次に、このドローアブルをTextView背景として使用します。

于 2012-12-05T18:26:00.803 に答える
0

9パッチまたはxmlを使用してマルチカラーボーダーEditTextを作成できます<layer-list> 。ここでは、マルチカラーボーダー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);

}

ソースandroid--コード

于 2016-06-25T11:14:16.693 に答える