android:通过在运行时膨胀来重用XML中的RelativeLayout?

发布于 2024-12-05 19:56:31 字数 5811 浏览 0 评论 0原文

问题的版本

它已经在一个单独的 xml (tablet_shortterm_column.xml) 中,请参见上面。

不管怎样,logcat 似乎抱怨 rlo_shortterm_col 已经有一个父级,所以它不允许另一个 ptr_rlo_rght_middle.addView(rlo_shortterm_col )。毫无意义。


我在这个问题上花了很多时间,但仍然无法解决。有人可以帮我一下吗?提前致谢。

我有一个 xml 文件 (tablet_shortterm_column.xml),其中包含我需要一次又一次重复使用的relativelayout。有时在同一个屏幕上多次水平地一个接一个地堆叠。我正在尝试将一个插入到现有的RelativeLayout中(即一个插入另一个中。)

//摘录

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>

Edition of question

It is already in a separate xml (tablet_shortterm_column.xml) see above.

Anyway, it seems logcat complains that rlo_shortterm_col already has a parent, so it won't allow an another ptr_rlo_rght_middle.addView(rlo_shortterm_col ). Makes no sense.


I spent so many hours on thsi problem and still can't solve it. Can someone please give me a hand? Thanks in advance.

I have an xml file (tablet_shortterm_column.xml) that contains a RelativeLayout that I need to re-use, again and again. Sometimes many times on the same screen stacked one after the other horizontally. I am attempting to insert one into an existing RelativeLayout (ie. one inside the other.)

//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

//********************* content of 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-12-12 19:56:31

为您的RelativeLayout 创建一个单独的xml 文件,然后根据需要多次膨胀它。

Create a separate xml file for your RelativeLayout and then inflate it as many times as you want.

马蹄踏│碎落叶 2024-12-12 19:56:31

llo_tmp 是您尝试重用的 RelativeLayout 的父视图。因此,您无法将其添加到另一个 ViewGroup 中,并且会收到该 logcat 错误。

您可以从 xml 文件中删除 LinearLayout 内容,并以相同的方式膨胀 xml 文件(尽管可能不是返回 View,而是返回 RelativeLayout< /代码>)。您不应该必须更改大部分 Java 代码,因为引用仍然相同。

或者,快速修复可能是将 llo_tmp 添加到您的 ViewGroup 而不是 rlo_tmp。不管怎样,rlo_tmp 已经有一个父级并且不能被重用。由于您没有让布局填充整个屏幕宽度,因此您可能不希望这样做。

llo_tmp is the parent view of the RelativeLayout you're trying to reuse. Thus, you can't add it to another ViewGroup and you get that logcat error.

You can remove the LinearLayout stuff from your xml file and inflate the xml file in the same way (though maybe instead of returned View you'd return a RelativeLayout). You shouldn't have to change much of the java code since the reference is still the same.

Or, a quick fix may be to add llo_tmp to your ViewGroup instead of rlo_tmp. Either way, rlo_tmp already has a parent and can't be reused. Since you don't have your layout fill the entire screen width, you probably don't want this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文