[英]My TextView Doesn't Seen
I want to see on the screen a Button
object, a TextView
object and a MyView
object. 我想在屏幕上看到一个
Button
对象,一个TextView
对象和一个MyView
对象。 But only appear Button
object and MyVıew
object. 但是仅出现
Button
对象和MyVıew
对象。 I think , MyView overlapping(above the TextView
) with TextView. 我认为MyView与TextView重叠(在
TextView
之上)。 Because If I don't add( b.addView(a);
) MyView
object to my layout, Button
and TextView
objects appear on the screen. 因为如果我不向布局中添加(
b.addView(a);
) MyView
对象,则Button
和TextView
对象会出现在屏幕上。 But If I add( b.addView(a);
) MyView , TextView is gone. 但是,如果我添加(
b.addView(a);
)MyView,TextView就消失了。 How can I solve this problem? 我怎么解决这个问题?
MyView.java file: 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
file : 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
file : 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>
Try with this activity_customer.xml: 尝试使用这个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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mehmet.catchtheball.main">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<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>
Add a LinearLayout as container below textView3 and add all to then 在textView3下面添加LinearLayout作为容器,然后将所有内容添加到
In the XML here you posted, add a with layout_belowOf=textView3, at your code where you do b.addView(a) replace with: LinearLayout c = findViewById(R.id.ll_c_container) and c.addView(a) 在此处发布的XML中,在执行代码的代码中添加一个带有layout_belowOf = textView3的b.addView(a)替换为:LinearLayout c = findViewById(R.id.ll_c_container)和c.addView(a)
If you want to all components to be in an linear axis you can have just one linearlayout (all addViews will add below the last view) 如果要使所有组件位于线性轴上,则只能有一个linearlayout(所有addViews都将添加到最后一个视图的下方)
<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>
In your java class just do this one (Sorry i am writing kotlin code 在您的Java类中只需执行此操作(对不起,我正在编写kotlin代码
MyView a = new MyView(this);
val params = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.BELOW, textView3.id)
params.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE)
relativeLayout.addView(textView, params)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.