我希望在屏幕上看到Button
对象,TextView
对象和MyView
对象。但只显示Button
对象和MyVıew
对象。我认为,MyView与TextView重叠(在TextView
之上)。因为如果我没有向我的布局添加(b.addView(a);
)MyView
个对象,屏幕上会显示Button
和TextView
个对象。但如果我添加(b.addView(a);
)MyView,TextView就不见了。我该如何解决这个问题?
MyView.java文件:
package com.example.mehmet.catchtheball;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyView extends View {
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("lightGray"));
canvas.drawCircle(500, 500, 150, paint);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
Customer.java
档案:
package com.example.mehmet.catchtheball;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class Customer extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
MyView a = new MyView(this);
RelativeLayout b = (RelativeLayout)findViewById(R.id.relativeLayout);
b.addView(a); // If I do this , TextView gone.
final TextView label = (TextView) findViewById(R.id.textView3);
label.setText("HelloEveryOne"); // My TextView
a.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
}
}
activity_customer.xml
档案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mehmet.catchtheball.main">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="undo"
android:text="@string/undo" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
tools:textColor="@android:color/background_dark" />
</RelativeLayout>
答案 0 :(得分:0)
尝试使用此activity_customer.xml:
{{1}}
答案 1 :(得分:0)
在textView3下面添加一个LinearLayout作为容器,然后将所有内容添加到
在你发布的XML中,在你的代码中添加一个layout_belowOf = textView3,b.addView(a)替换为:LinearLayout c = findViewById(R.id.ll_c_container)和c.addView(a)
如果您希望所有组件都在线性轴上,您只能有一个linearlayout(所有addViews将添加到最后一个视图下方)
答案 2 :(得分:0)
map.get(key)[0] = "test"
在你的java类中只需执行此操作(抱歉,我正在编写kotlin代码
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="undo"
android:text="@string/undo" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/button"
android:layout_marginStart="27dp" />
</RelativeLayout>