Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 layout Android使布局可重用_Android Layout - Fatal编程技术网

Android layout Android使布局可重用

Android layout Android使布局可重用,android-layout,Android Layout,我对复制这段代码感到厌烦 <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="6dp" android:orientation="vertical"> <!-- stuff goes here -- usually

我对复制这段代码感到厌烦

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:orientation="vertical">

<!-- stuff goes here -- usually more layouts -->

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:textStyle="bold"
                android:background="@color/background"
                android:paddingLeft="6dp"
                android:paddingRight="6dp"
                android:layout_marginBottom="6dp"
                android:text="Title"
                android:layout_marginTop="-3dp"
                />
        </RelativeLayout>


有没有一种方法可以让它重复使用,比如:

<BoxWithTitleLayout
  android:layout_weight="1"
  title="Title"
>
<!-- stuff -->
</BoxWithTitleLayout>

您可以使用[!LayoutInflater]()根据布局模板创建视图,然后将其插入到需要的视图中

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);

// fill in any details dynamically here
TextView textView = (TextView) 
v.findViewById(R.id.a_text_view);
textView.setText("your text");

// insert into main view
ViewGroup insertPoint = (ViewGroup) 
findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

可以使用[!LayoutInflater]()基于布局模板创建视图,然后将其插入到需要的视图中

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);

// fill in any details dynamically here
TextView textView = (TextView) 
v.findViewById(R.id.a_text_view);
textView.setText("your text");

// insert into main view
ViewGroup insertPoint = (ViewGroup) 
findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));