Редакция вопроса
Он уже находится в отдельном xml (tablet_shortterm_column.xml), см. Выше.
В любом случае, logcat жалуется, что у rlo_shortterm_col уже есть родитель, поэтому он выиграл 'Разрешить другой ptr_rlo_rght_middle.addView (rlo_shortterm_col).Не имеет смысла.
Я потратил так много часов на эту проблему и до сих пор не могу ее решить.Может кто-нибудь, пожалуйста, помогите мне?Заранее спасибо.
У меня есть XML-файл (tablet_shortterm_column.xml), который содержит RelativeLayout, который мне нужно использовать повторно, снова и снова.Иногда много раз на одном экране накладываются один за другим горизонтально.Я пытаюсь вставить один в существующий RelativeLayout (т.е. один внутри другого.)
// exerpts
public class TabletMain extends Activity {
setContentView(R.layout.tablet_main);
public RelativeLayout ptr_rlo_rght_middle;
ptr_rlo_rght_middle = (RelativeLayout) findViewById(R.id.rlo_rght_middle);
//rlo_rght_middle is in tablet_main.xml
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View llo_tmp = (View) inflater.inflate(R.layout.tablet_shortterm_column,null);
RelativeLayout rlo_tmp = (RelativeLayout) llo_tmp.findViewById(R.id.rlo_shortterm_col);
// rlo_shortterm_col is the object I want to reuse it a RelativeLayout and is inside
// tablet_shortterm_column.xml
RelativeLayout.LayoutParams rlo_layoutparams;
rlo_layoutparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlo_layoutparams.addRule(RelativeLayout.RIGHT_OF, R.id.llo_rght_middle_col1);
// llo_rght_middle_col1 is a RelativeLayout inside tablet_main.xml,
// I want to put another RelativeLayout view right next to it.
rlo_tmp.setLayoutParams(rlo_layoutparams);
ptr_rlo_rght_middle.addView(rlo_tmp); //Application crashes right on this line.
} //end Activity
// ********************* содержание tablet_shortterm_column.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/rlo_shortterm_col"
android:layout_width="180dp"
android:layout_height="fill_parent"
android:background="#436699"
android:orientation="vertical"
android:layout_margin="3px"
android:weightSum="1"
> <!-- android:background="#32CD32" android:layout_height="365dp" android:layout_margin="30px" -->
<Button
android:id="@+id/btn_shortterm_col"
android:layout_alignParentTop="true"
android:text="Tuesday Afternoon"
android:layout_margin="15px"
android:textSize="12px"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#296699"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
> <!--android:background="#32CD32" -->
</Button>
<ImageView
android:id="@+id/iv_shortterm_col"
android:layout_below="@+id/btn_shortterm_col"
android:src="@drawable/tblet_icon14_med"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
><!-- android:src="@drawable/tblet_shape1" android:layout_gravity="center_horizontal" -->
</ImageView>
<TextView
android:id="@+id/tv_shortterm_col1"
android:layout_below="@+id/iv_shortterm_col"
android:text="-10ºC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:background="#DCDCDC"
android:textColor="#000000"
android:textSize="12px"
android:layout_centerHorizontal="true"
> <!-- android:layout_gravity="center_horizontal" -->
</TextView>
<TextView
android:id="@+id/tv_shortterm_col2"
android:layout_below="@+id/tv_shortterm_col1"
android:text="Flurries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:background="#DCDCDC"
android:textColor="#000000"
android:textSize="12px"
android:layout_centerHorizontal="true"
>
</TextView>
<RelativeLayout
android:id="@+id/rlo_shortterm_col_1"
android:layout_below="@+id/tv_shortterm_col2"
android:src="@drawable/tblet_shape2"
android:background="#32CD32"
android:layout_height="113dp"
android:layout_margin="40px"
android:layout_width="125dp"
> <!--android:background="#32CD32" android:orientation="vertical" -->
</RelativeLayout>
</RelativeLayout>
</LinearLayout>