Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将一系列重复的视图组件提取到自己的视图中的最佳方法?_Android_Refactoring_Android View_Android Ui - Fatal编程技术网

Android 将一系列重复的视图组件提取到自己的视图中的最佳方法?

Android 将一系列重复的视图组件提取到自己的视图中的最佳方法?,android,refactoring,android-view,android-ui,Android,Refactoring,Android View,Android Ui,在我想创建的一个基本游戏应用程序中,我有一个菜单屏幕,上面有一系列框,每个框代表用户可以选择的级别 这些框是RelativeLayouts,其中包含一些打包的其他元素,如一些TextViews、一个ProgressBar和一个按钮 由于我的布局中有许多这样的框,我想知道,有没有办法将每个框提取到它自己的视图中?我觉得这会使我的应用程序更干净、更有条理,并且有利于实践 我研究了“创建自定义视图”,但它似乎包含了许多复杂的度量,我还没有找到任何关于将现有视图包含到我的视图中的内容(例如,在我的自定义

在我想创建的一个基本游戏应用程序中,我有一个菜单屏幕,上面有一系列框,每个框代表用户可以选择的级别

这些框是
RelativeLayout
s,其中包含一些打包的其他元素,如一些
TextView
s、一个
ProgressBar
和一个
按钮

由于我的布局中有许多这样的框,我想知道,有没有办法将每个框提取到它自己的
视图中
?我觉得这会使我的应用程序更干净、更有条理,并且有利于实践

我研究了“创建自定义视图”,但它似乎包含了许多复杂的度量,我还没有找到任何关于将现有视图包含到我的视图中的内容(例如,在我的自定义视图中包含
ProgressBar

这是一个截图,让你知道我在做什么


自定义视图听起来像是解决方案。我假设除了通用xml之外,您还有一个通用的功能(当按下“go”等时)

我要做的是提取公共布局并扩展一个相对布局,这将使该布局膨胀

事实上,在这里之前已经得到了回答:

您可以使用android notes:

编辑2(可选,包括标签):

这种方法还从将公共布局提取到另一个xml开始。使用标记include(或merge)可以将xml注入布局中任何需要的位置。UI“组件”看起来是一样的,在大多数情况下,它们可能会满足您的需求

有关详情,请访问:

编辑1(扩展视图,带示例代码):

步骤1-定义视图的布局-这是常见的UI部分,只是一组视图

请注意,使用合并标记可以在布局层次结构中保存一个级别

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="wrap_content"
    android:background="@android:color/holo_green_dark"
    android:layout_height="wrap_content">

<TextView
    android:id="@+id/txtLevel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:text="Level"/>

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_centerHorizontal="true"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtLevel"/>

<Button
    android:id="@+id/btnGo"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/progressBar"
    android:text="Go"/>

</RelativeLayout>
步骤3-在应用程序的其他布局中使用视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">

    <com.example.drorfichman.stuff.SampleCustomView
        android:id="@+id/myView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.example.drorfichman.stuff.SampleCustomView
        android:id="@+id/myView2"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
结果(哦,恐怖)


尝试将此视图层次结构提取为片段并重新使用?实际上,我在google中没有找到示例。希望能给我们俩留点时间,我猜它藏起来了。。尽量使它简单,与你的问题相关。这里真的没有什么令人惊讶的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">

    <com.example.drorfichman.stuff.SampleCustomView
        android:id="@+id/myView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.example.drorfichman.stuff.SampleCustomView
        android:id="@+id/myView2"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        // This is one of the views
        SampleCustomView v = (SampleCustomView)findViewById(R.id.myView);
    }
}