Android agrega borde para editar texto mediante programación
Utilicé este ejemplo e intenté agregarlo a mi texto de edición mediante programación, como editText.setBackgroundResource(R.drawable.edit_text_back);
, Pero no funciona. ¿Cómo puedo lograr esto? ¿Alguna sugerencia o idea?
EDIT El editText también se define mediante programación.
- Aplicación para Android: Imagen de fondo sobre el color de fondo
- Cambios en el color de fondo de Android ListView en el desplazamiento - colorCacheHint No se soluciona el problema
- Fragmento tiene el fondo blanco por defecto en replace () en lugar de uno transparente
- La flecha giratoria desaparece cuando cambio el color de fondo
- Establecer el borde y el color de fondo de textView
EditText editText = new EditText(this.getApplicationContext());
He añadido esto a una fila de la tabla
INTENTÓ
editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back)); editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_text_back));
EDITAR CREACIÓN DE TEXTO
TableRow row = (TableRow) findViewById(R.id.table_row_kind); TableRow.LayoutParams rowP = new TableRow.LayoutParams(); rowP.setMargins(10, 0, 0, 0); editText = new EditText(this.getApplicationContext()); editText .setGravity(Gravity.FILL_HORIZONTAL); editText .setLayoutParams(rowP); editText .setFilters(new InputFilter[]{txtFilter}); editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));
Row.xml
<TableRow android:id="@+id/table_row_kind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dip" > <TextView android:layout_width="250sp" android:text="Kind" android:textAppearance="?android:attr/textAppearanceLarge" /> </TableRow>
- Establecer color del área 'vacío' de ListView en Android
- ¿Cómo puedo cambiar el color de fondo de una fila específica correctamente en un ListView? (Androide)
- configurar el color de fondo en listview
- ¿Cambia el color de fondo en android destruir el aspecto del widget?
- Android: ¿Cómo usar colores personalizados?
- ¿Cómo puedo convertir a Color a Bitmap?
- Animar el cambio de color de fondo de la vista en Android
- Cambiar el color de fondo del widget de la aplicación por programación
Bueno, también tengo el mismo problema que resolver por la siguiente manera. Su es un archivo xml ponerlo en su carpeta dibujable y establecer este xml en el fondo de que EditText
Código de actividad:
EditText foo = (EditText)findViewById(R.id.editText); foo.setBackgroundResource(R.drawable.backtext);
Backtext.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ffffff" /> <stroke android:width="1dip" android:color="#000000"/> </shape>
Crear un archivo edittext.xml en una carpeta dibujable
<?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp"> <solid android:color="#FFFFFF"/> <stroke android:width="1dp" android:color="@android:color/black" /> <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/> </shape> in your main.xml <EditText background="drawable/edittext.xml" />
Este código está funcionando para dibujar la frontera a cualquier vista programáticamente
package com.example.border; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.ShapeDrawable; public class ShapeDrawableWithoutBottom extends ShapeDrawable { private float mLineWidth = 1f; private final Paint mLinePaint; private int color; public ShapeDrawableWithoutBottom() { // No color specified, so call constructor with default color White this(Color.WHITE); } public ShapeDrawableWithoutBottom(int layoutColor) { // use the setter defined below, to set the main color for this drawable // setColor(color); setColor(layoutColor); // setup the Paint for drawing the lines mLinePaint = new Paint(); mLinePaint.setStyle(Paint.Style.STROKE); mLinePaint.setStrokeWidth(mLineWidth); } public void setColor(int color) { Paint paint = getPaint(); paint.setColor(color); } public void setLineColor(int color) { this.color = color; } public void setLineWidth(float lineWidth) { mLineWidth = lineWidth; mLinePaint.setStrokeWidth(mLineWidth); } @Override public void draw(Canvas canvas) { super.draw(canvas); // bottom black line // ////////////////// mLinePaint.setColor(Color.parseColor("#00000000")); mLinePaint.setAlpha((int) (255 * 0.0)); // Opacity 90% canvas.drawLine(getBounds().left, getBounds().bottom - mLineWidth * 0.5f, getBounds().right, getBounds().bottom - mLineWidth * 0.5f, mLinePaint); // translucent grey rim // ///////////////////// mLinePaint.setColor(color); mLinePaint.setAlpha((int) (255 * 0.7)); // Opacity 70% // top canvas.drawLine(getBounds().left, getBounds().top + mLineWidth * 0.5f, getBounds().right, getBounds().top + mLineWidth * 0.5f, mLinePaint); // left canvas.drawLine(getBounds().left + mLineWidth * 0.5f, getBounds().bottom , getBounds().left + mLineWidth * 0.5f, getBounds().top + mLineWidth, mLinePaint); // right canvas.drawLine(getBounds().right - mLineWidth * 0.5f, getBounds().bottom , getBounds().right - mLineWidth * 0.5f, getBounds().top + mLineWidth, mLinePaint); // top white line // /////////////// mLinePaint.setColor(Color.WHITE); mLinePaint.setAlpha((int) (255 * 0.5)); // Opacity 50% canvas.drawLine(getBounds().left + mLineWidth, getBounds().top + mLineWidth * 1.5f, getBounds().right - mLineWidth, getBounds().top + mLineWidth * 1.5f, mLinePaint); } }
Pruebe esto en esto he añadido dinámicamente edittext a continuación, establecer su fondo y funciona.
LinearLayout layout=(LinearLayout)findViewById(R.id.layout); EditText edit=new EditText(MainActivity.this); edit.setBackgroundResource(R.drawable.abc); edit.setMaxWidth(100); edit.setMinHeight(100); edit.setText("hello"); layout.addView(edit);
Utilizar
editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));
Instated de
editText.setBackgroundResource(R.drawable.edit_text_back);
Esto es lo que puede hacer el borde para el texto de edición. Guardar esto en res / drawable
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle"> <gradient android:startColor="#f9f9f9" android:centerColor="#ffffff" android:endColor="#ffffff" android:angle="90"/> <stroke android:width="1dp" android:color="#2B547E"/> </shape> </item> </layer-list>