J'ai un FrameLayout dans lequel j'ai 2 contrôles: - une vue personnalisée qui tire une image et un texte sur elle - un textview avec un textevue centre Android dans FrameLayout ne fonctionne pas
Je veux Centrer les deux dans le FrameLayout mais je n'arrive pas à le faire. Le Texview est bien centré, ma vue cusom reste sur le côté gauche, quand je la rends visible.
<FrameLayout android:id="@+id/CompassMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<view class="com.MyView"
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="CENTERED" />
</FrameLayout>
Pour Mathias, je ne fais rien dans le constructeur, il est tout simple
public class MyMapView extends View {
private int xPos = 0;
private int yPos = 0;
private Bitmap trackMap;
private Matrix backgroundMatrix;
private Paint backgroundPaint;
private Bitmap position;
private Matrix positionMatrix;
private Paint positionPaint;
public MyMapView(Context context) {
super(context);
init(context, null);
}
public MyMapView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MyMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(final Context context, AttributeSet attrs) {
backgroundMatrix = new Matrix();
backgroundPaint = new Paint();
backgroundPaint.setFilterBitmap(true);
position = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.position);
positionMatrix = new Matrix();
positionPaint = new Paint();
positionPaint.setFilterBitmap(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}
@Override
protected void onDraw(Canvas canvas) {
int width = getMeasuredWidth();
int height = getMeasuredHeight();
if (trackMap!=null)
{
Bitmap resizedBitmap = Bitmap.createScaledBitmap(trackMap, height, height, true);
canvas.drawBitmap(resizedBitmap, backgroundMatrix, backgroundPaint);
}
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.translate(xPos-position.getWidth()/2, yPos-position.getHeight()/2);
canvas.drawBitmap(position, positionMatrix, positionPaint);
canvas.restore();
}
public void updatePosition(int xpos, int ypos, Bitmap trackImage)
{
xPos=xpos;
yPos=ypos;
trackMap = trackImage;
invalidate();
}
}
D'où provient votre vue personnalisée? Bien sûr, vous manipulez les paramètres dans votre constructeur? –
Y a-t-il une raison particulière pour laquelle vous devez utiliser un FrameLayout et le TextView doit-il être affiché en haut de la vue? –
Oui, le FrameLayout ne vous laissera pas faire cela. C'est vraiment juste pour tenir une seule vue. Si vous ajoutez plus à cela que cela, il aura le comportement exact que vous mentionnez, il ajoute simplement des éléments dans une pile verticale en épinglant en haut à gauche. http://developer.android.com/reference/android/widget/FrameLayout.html – plainjimbo