如何在Android中创建子视图?

如何在Android中创建子视图?,android,Android,我是android新手,我有一个基本问题:我想创建一个包含2个按钮的子视图,我想在相对布局中添加这个子视图。如何使用100%的xml文件实现这一点?如果我不能在xml中执行此操作,我将等待您的建议。我必须绝对指出,我想要这个单独的视图,包含两个按钮。谢谢。如果您想要的是视图中的两个按钮,即相对视图中的两个按钮,那么您有: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http:

我是android新手,我有一个基本问题:我想创建一个包含2个按钮的
子视图,我想在相对布局中添加这个子视图。如何使用100%的
xml
文件实现这一点?如果我不能在
xml
中执行此操作,我将等待您的建议。我必须绝对指出,我想要这个单独的视图,包含两个按钮。谢谢。

如果您想要的是
视图
中的两个
按钮,即
相对视图
中的两个按钮,那么您有:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/viewToInject"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button01"
            android:visibility="visible" />

        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:visibility="visible" />

    </LinearLayout>

</RelativeLayout>

我认为他的意思只是一个简单的视图……或者你最好指出这个“子视图”的功能应该是什么。它总是可见的,还是你想做一个像弹出窗口或AlertDialog这样的东西

无论如何,您可以按照自己的意愿构建纯XML中提到的joao2fast4u布局。当然,您永远无法处理非XML中的视图可见性。如果你想制作一个“弹出窗口”,通常在Android中称为AlertDialog,你需要Java代码

请先解释一下你的愿望,以便更好地帮助你

Android编程中没有使用“子视图”这一措辞,因此我们真的不知道您想问什么

这是您想要的,但基本上是无用的,您在Android Studio中也得到一个提示,其中一个布局没有任何用途

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

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:id="@+id/button3"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/button2" />
</LinearLayout>


好的,我明白这一点。但是现在,我如何将这个视图(button1+button2)注入到另一个视图中呢?您必须使用addView()方法来实现这一点。但是你只能在视图组中使用它。谢谢你!但我不明白为什么要和ViewGroup分手。视图组必须是什么?视图组是一种特殊视图,可以包含其他视图(称为子视图)。基本上,线性布局、相对布局或框架布局是一种视图组布局。“在子视图中查看更多”是iOS的一个概念,我认为在android中被称为child,但我不确定。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:id="@+id/button3"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/button2" />
</LinearLayout>