2010-07-26 6 views
1

J'ai une disposition personnalisée qui dessine un rectangle arrondi transparent sous ses enfants. Le problème est quand j'essaye de l'ajouter à mon dossier de xml, il n'apparaît pas. En outre, lorsque j'essaie d'ajouter des paramètres (c'est-à-dire android:layout_width), la fenêtre contextuelle indique qu'aucun d'entre eux n'est disponible. La même chose arrive à toutes les vues enfant que j'ajoute. Quelqu'un peut-il aider?Écriture personnalisée LinearLayout pour android

public class RoundRectLayout extends LinearLayout 
{ 
private RectF shape; 
public RoundRectLayout(Context context) 
{ 
    super(context); 
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.settings, this); 
    shape = new RectF(); 
} 

public RoundRectLayout(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.settings, this); 
    shape = new RectF(); 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) 
{ 
    shape = new RectF(0, 0, w - 5, h - 5); 
    super.onSizeChanged(w, h, oldw, oldh); 
} 

@Override 
protected void dispatchDraw(Canvas canvas) 
{ 
    Paint temp = new Paint(); 
    temp.setAlpha(125); 
    canvas.drawRoundRect(shape, 10f, 10f, temp); 
    super.dispatchDraw(canvas); 
} 
} 

Répondre

8

Si vous voulez juste un fond de rectangle arrondi pour votre LinearLayout, vous pouvez définir une forme drawable dans un fichier xml dans « res/drawable/»:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#50000000"/>  
    <stroke android:width="3dp" 
      android:color="#ffffffff"/> 
    <padding android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp" 
      android:bottomLeftRadius="7dp" 
      android:topLeftRadius="7dp" 
      android:topRightRadius="7dp"/> 
</shape> 

Ensuite, vous définissez l'arrière-plan de la mise en page au nom de votre fichier XML dessinable. Si vous l'avez nommé round_border.xml, comme arrière-plan:

<LinearLayout 
    android:background="@drawable/round_border" 
    ... 

La transparence est définie ci-après ...

<solid android:color="#50000000"/>  

2 premiers chiffres désigne la transparence, 6 derniers chiffres est la couleur.

0

Je pense que c'est le plugin qui échoue mais vous pouvez spécifier android: layout_ *, il sera pris en compte. Quoi qu'il en soit, si vous voulez juste dessiner une mise en page rect tour, suivez les instructions de Marco

1

Ce tuto m'a vraiment aidé à yourp roblème

d'abord, vous devez définir les champs que vous souhaitez passer à votre disposition personnalisée dans un res de fichiers/valeurs/attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyLayout"> 
     <attr name="text" format="string" /> 
    </declare-styleable> 
</resources> 

maintenant, préparez votre constructeur à recive ces attributs

public RoundRectLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    TypedArray a = context.obtainStyledAttributes(attrs, 
      R.styleable.MyLayout); 
    CharSequence s = a.getString(R.styleable.RoundRectLayout_text); 
    if (s != null) { 
     mText = s.toString(); 
    } 
    a.recycle(); 
} 

Ensuite, ajoutez un espace de noms à votre la yout.xml afin que konws android devaient chercher votre disposition personnalisée. "mi.pakage" est le paquet java où votre mise en page personnalisée est.

Une fois que vous avez votre espace de nom et votre classe il suffit d'appeler les atributs définies dans attrs.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:myapp="http://schemas.android.com/apk/res/mi.pakage" 
    android:id="@+id/root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#fff"> 

    <com.example.MyLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     myapp:text="My Special Text String"/> 

</RelativeLayout> 

Mais si vous voulez juste pour dessiner un rectangle, vous pouvez utiliser la méthode sugested dans l'autre réponse