0

我的帖子是基于以前的帖子,并且大大简化了。

Android:使用 XML 相互叠加的两个视图

文件/对象 DrawV 用粉红色的圆圈填充屏幕,并允许触摸一个圆圈使其消失。在另一个文件中,private DrawV drawView = new DrawV(this); 这会填充屏幕但不参与布局。

setContentView(drawView) 显示点,所以我知道它有效。我想使用名为 setContentView(R.layout.activity_title); 的布局 其中包括屏幕顶部的两个按钮和下方的点。换句话说,我想知道是否有一种方法可以将显示的点放在某种视图中,该视图可以包含在同一布局中的按钮中。

有什么帮助吗?请?

告诉我你是否需要什么。

4

1 回答 1

0

If DrawV is an Android View (or extends View), you can include it in a regular xml layout file, and then use that layout file with setContentView(int).

To reference the DrawV class in your layout, you'll need to use the fully-qualified name (with the package).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <LinearLayout
    android:id="@+id/buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
      android:id="@+id/button_one"
      android:text="One"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1.0" />

    <Button
      android:id="@+id/button_two"
      android:text="Two"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1.0" />
  </LinearLayout>

  <com.example.views.DrawV
    android:layout_below="@id/buttons"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

Above, the RelativeLayout is your root view. The LinearLayout, buttons, is a ViewGroup just to hold the two buttons and keep them of equal width (note the layout_width=0dp and equal layout_weight). Your DrawV view will be laid out below the buttons View, and then will match the parent container's width and height (fill it).

If you save this under src/main/res/layout/activity_circles.xml, you'll be able to use setContentView(R.layout.activity_circles) in your Activity to set the layout.

于 2014-07-13T01:39:43.883 回答