簡體   English   中英

如何在Android中定義EditText的不同顏色和寬度

[英]How to define a different color and width of an EditText in Android

這就是我向EditText添加邊框的方法。 如何只在EditText 3個邊上添加邊框,並為每個邊框定義不同的顏色和寬度?

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);
}

我在下面的嘗試不起作用:

<?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_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>

現在,將此可繪制對象設置為您的EditText背景:

android:background="@drawable/my_edittext_border"

將此可繪制對象設置為背景,並根據您的選擇對其進行修改:

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM