问题
I want to display two views on a screen. One is a randomly positioned dot bitmaps. I can get that do display with setContentView(...). I also have two bitmaps and a textview that are arranged via xml layout language and then using setContentView(R.layout.activity_title);
DrawV.java !!!BASICALLY I THINK I HAVE TWO setContentViews(...) THAT I WANT TO DISPLAY AT THE SAME TIME. TELL ME IF YOU WANT MORE INFORMATION.
public class DrawV extends View { VARIABES...
public DrawV(Context context) { CREATE CIRCLES (CIRCLE OBJECTS) STORING COORDINATES RANDOMLY CHOSEN...
@Override protected void onDraw(Canvas canvas) { DRAW THESE CIRCLES AS BITMAPS...SCREEN IS NOW RANDOMLY COVERED WITH MANY DOTS }
GameActivity.java
public class GameActivity extends Activity { private DrawV drawView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawV(this);
setContentView(R.layout.activity_title); !!!SHOWS LAYOUT (HOPEFULLY TEXTVIEW, TWO BUTTONS, AND RANDOMLY POSITIONED DOTS (DOTS NEVER WORKS) WHICH IS OVERDONE BY DRAWVIEW...
setContentView(drawView); !!!IF THIS STATEMENT IS SECOND IT RENDERS INTEAD OF THE PREVIOUS STATEMENT. OTHERWISE activity_title shows.
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
DEFAULT CODE...
}
}
!!!THE WORST OFFENDER: activity_title.xml
activity_title.xml
!!!I AM NOT SURE HOW TO WRITE THIS. IT DOESN'T CURRENTLY WORK. I want to display two views on a screen. One is a randomly positioned dot bitmaps. I can get that do display with setContentView(...). I also have two bitmaps and a textview that are arranged via xml layout language and then using setContentView(R.layout.controls...);
DrawV.java !!!BASICALLY I THINK I HAVE TWO setContentViews(...) THAT I WANT TO DISPLAY AT THE SAME TIME. TELL ME IF YOU WANT MORE INFORMATION.
public class DrawV extends View { VARIABES...
public DrawV(Context context) { CREATE CIRCLES (CIRCLE OBJECTS) STORING COORDINATES RANDOMLY CHOSEN...
@Override protected void onDraw(Canvas canvas) { DRAW THESE CIRCLES AS BITMAPS...SCREEN IS NOW RANDOMLY COVERED WITH MANY DOTS }
GameActivity.java !!!STARTING .JAVA
public class GameActivity extends Activity { private DrawV drawView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawV(this);
setContentView(R.layout.activity_title); !!!SHOWS LAYOUT (HOPEFULLY TEXTVIEW, TWO BUTTONS, AND RANDOMLY POSITIONED DOTS (DOTS NEVER WORKS) WHICH IS OVERDONE BY DRAWVIEW...
setContentView(drawView); !!!IF THIS STATEMENT IS SECOND IT RENDERS INTEAD OF THE PREVIOUS STATEMENT. OTHERWISE activity_title shows.
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
DEFAULT CODE...
}
}
!!!THE WORST OFFENDER: activity_title.xml
activity_title.xml
!!!I AM NOT SURE HOW TO WRITE THIS. IT DOESN'T CURRENTLY WORK.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
android:textColor="#ff0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="40sp"
android:text="@string/timer"
android:id="@+id/timer">
</TextView>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginLeft="20dp"
android:background="@drawable/stopbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="@string/stopText"
android:id="@+id/stopButton"
android:visibility="gone">
</Button>
<Button
android:layout_marginLeft="20dp"
android:background="@drawable/startbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="@string/startText"
android:id="@+id/startButton">
</Button>
<Button
android:layout_marginRight="20dp"
android:background="@drawable/resetbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="@string/resetText"
android:id="@+id/resetButton">
</Button>
</LinearLayout>
</LinearLayout>
<LinearLayout !!!MESSED UP CODE. DON'T KNOW HOW TO SHOW DOTS USING XML
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<View class="com.category.tap.DrawV"
android:id="@+id/dotpic"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
If anyone needs more information, let me know.
回答1:
Inside the XML
file, declare both Views
inside a FrameLayout
, the one that should be in top of the other, should be declared last.
To declare a custom created view, use the following format:
<com.mypackage.MyCustomView
android:layout_width=......
回答2:
If you want to stack both of your layouts, first add them to a FrameLayout.
回答3:
For stacking views relaivelayout or framelayout can be used. These layout support z-index placement so the views can overlap eachother. You cannot have two setcontentview in same activity. To show a dot on screen you can do this too:
<View android:layout_height="1dp"
android:layout_width="1dp"
android:background="@android:color/black"/>
来源:https://stackoverflow.com/questions/24131211/android-two-views-on-top-of-each-other-using-xml