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

标签 android xml android-relativelayout

问题的版本

它已经在单独的 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>

最佳答案

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

关于android:通过在运行时膨胀它来重用 XML 中的 RelativeLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524593/

相关文章:

python - 使用 Python 在 XML 中的属性中查找和替换特定文本

java - 如何删除android布局中的图片

java - 根据按下的按钮显示信息

android - 如何使用 Android Room 进行单行查询

java - 在 Android 中测试时使用不同的 `google-services.json`

android - 如何将一些 fragment 添加到 TabHost 中?

c# - Json.net:JObject.SelectToken 可以做 XPath 可以做的事情吗?如果是,语法是什么?

xml - Swift XML 解析器随机删除空格

android - 如何在 android 中的一个布局上显示一半按钮,在另一个布局上显示一半按钮?

java - 如何以编程方式添加 GestureOverlayView?