काम नहीं करता मैं एक FrameLayout जिसमें मैं 2 नियंत्रण है है: - जो उस पर एक छवि और कुछ टेक्स्ट ड्रॉ एक कस्टम दृश्य - एक पाठFrameLayout में एंड्रॉयड केंद्र दृश्य
मैं चाहता हूँ के साथ एक TextView फ्रेमलेआउट में दोनों को केंद्रित करने के लिए, लेकिन मैं इसे करने में सक्षम नहीं हूं। टेक्सव्यू बस ठीक है, जब मैं इसे दृश्यमान करता हूं, तो मेरा क्यूसम दृश्य बाईं तरफ रहता है।
<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>
मैथियास के लिए, मैं निर्माता में कुछ नहीं करते, यह मैं एक FrameLayout के बजाय एक RelativeLayout सुझाव देंगे सिर्फ सरल
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();
}
}
आपका कस्टम व्यू कहां से बढ़ाया गया है? निश्चित रूप से आप अपने कन्स्ट्रक्टर में पैरामीटर को संभालने वाले हैं? –
क्या कोई विशेष कारण है कि आपको फ़्रेमलेआउट का उपयोग करना है और व्यू के शीर्ष पर प्रदर्शित होने के लिए टेक्स्ट व्यू है? –
हाँ, फ़्रेमलेआउट आपको ऐसा करने नहीं देगा। यह वास्तव में सिर्फ एक दृश्य रखने के लिए है। यदि आप इससे अधिक जोड़ते हैं तो इसका सटीक व्यवहार होगा जिसका आप उल्लेख कर रहे हैं, यह ऊपरी बाएं को पिन करके केवल ऊर्ध्वाधर स्टैक में तत्व जोड़ता है। http://developer.android.com/reference/android/widget/FrameLayout.html – plainjimbo