我想弄清楚如何製作一個自定義的EditText,它沿着它的右側和左側,頂部是綠色邊框,底部是藍色邊框。見下: 多彩色的邊框EditText
我對Android開發相當新,我花了很多時間閱讀他們的文檔,但一直沒有找到任何關於這種定製的運氣。我知道在CSS中你可以使用border-right,border-left等屬性,但不確定在Android開發中它是否直截了當。我正在尋找兼容性最好的解決方案,最好從版本2.3(薑餅)開始。
我想弄清楚如何製作一個自定義的EditText,它沿着它的右側和左側,頂部是綠色邊框,底部是藍色邊框。見下: 多彩色的邊框EditText
我對Android開發相當新,我花了很多時間閱讀他們的文檔,但一直沒有找到任何關於這種定製的運氣。我知道在CSS中你可以使用border-right,border-left等屬性,但不確定在Android開發中它是否直截了當。我正在尋找兼容性最好的解決方案,最好從版本2.3(薑餅)開始。
您必須製作一張自定義圖片作爲背景。這是相當直接的,你會想要使用in the 2D graphics guide描述的9補丁。
一旦你的,你會把它放在你的項目的RES /可繪製文件夾,然後在XML與EditText上使用它作爲
<EditText
android:background="@drawable/my_custom_background"
...
/>
創建LayerList
用在彩色的正方形想要的梯度,並在它上面有一些邊界的白色正方形。然後使用這個可繪製的背景作爲你的TextView
背景。
你能給我一個代碼示例嗎?你的解釋聽起來讓我很困惑。 –
您可以使用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);
}
我傾向於從圖像解決方案遠離這樣的,因爲我怕圖像質量的失真取決於有多大或者多小,它需要被拉長,以及作爲視網膜的處理。 –
9patch的可伸縮區域由您專門標記,所以只要您做得正確,就不會出現任何扭曲。對於你想要的簡單邊框,即使你將它貼在100英尺寬360,000像素長的顯示器上,也不會有任何變形。 – Ralgha