【问题标题】:Android Layout CloningAndroid 布局克隆
【发布时间】:2014-04-24 06:01:53
【问题描述】:
<RelativeLayout
    android:id="@+id/component1"
    android:layout_width="200dp"
    android:layout_height="wrap_content" 
    android:layout_marginLeft="50dp"
    android:layout_marginTop="50dp"
    android:background="@drawable/my_shape">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher" />   

    <RelativeLayout
        android:id="@+id/component2"
        android:layout_width="200dp"
        android:layout_height="wrap_content" 
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp"
        android:background="@drawable/my_shape"
        android:visibility="gone">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_launcher" />      

        </RelativeLayout>       

</RelativeLayout>

android:id="@+id/component2" 能见度消失了;我想附上 android:id="@+id/component1" 在运行时;不是单个,而是 N 个组件 2(更改属性)

我的意思是,我想克隆component2

谁能帮我截取代码?

提前致谢。

【问题讨论】:

  • 我不太明白,你为什么不能用ListView
  • @Sebastien;这只是一个模板。在我的实际要求中,我在组件 1 中有 {TextView, EditText, 3 ImageButton},在组件 2 中有 {2 ImageButton, EditText}
  • 即使对不同的行有两种不同的布局,您仍然可以使用 ListView 并在 ListAdapter 中使用不同的 ViewType。

标签: android layout cloning


【解决方案1】:

我认为您正在寻找重用布局

您可以在一个布局中创建所有组件并使用include 标签调用它

<include layout="@layout/component"/>
<include layout="@layout/component"/>
<include layout="@layout/component"/>

link here

【讨论】:

  • \@Fanos;抱歉,但我不是在寻找重用布局说,layout1.xml,layout2.xml,...layoutN.xml 并在 2,3,...N 中使用 layout1.xml,而不是我想在布局中重用内部视图使用 findViewById() 的代码,但问题是假设我得到 view1 和 view2 并对其进行一些更改,但我认为两者都是指向同一个对象的引用;换句话说,一个人的变化会反映在另一个人身上……这就是我寻找克隆的原因。
【解决方案2】:

即使对不同的行使用两种不同的布局,您仍然可以使用 ListView 并在 ListAdapter 中使用不同的 ViewType。

另一种方法是在布局 xml 中仅包含一个容器布局(例如,LinearLayout 包装在 ScrollView 中)并以编程方式添加组件。您可以在单独的布局 xml 和代码中拥有组件的布局,将该布局 xml 扩展为视图(或 ViewGroup),设置文本或其他内容,最后将组件视图添加到容器视图。

如果其中一个建议对您有意义,我也可以提供代码示例。

【讨论】:

  • \@Ridcully;这只是一个模板。在我的实际要求中,我在组件 1 中有 {TextView, EditText, 3 ImageButton},在组件 2 中有 {2 ImageButton, EditText} ... ListView 可能
  • 我不确定你想要达到什么目的?在您的示例中,component-1 包含在 component-2 中。您是否希望 component-1 部分位于顶部,而 component-2 部分位于其下方?或者您是否需要交替或以某种方式混合的组件 1 部分和组件 2 部分的列表?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
相关资源
最近更新 更多