【问题标题】:Dynamically adding a relative layout into an activity inheriting it's property from a generic layout [duplicate]将相对布局动态添加到从通用布局继承其属性的活动中[重复]
【发布时间】:2013-11-14 06:42:13
【问题描述】:

我的布局文件夹中有一个相对布局,其中包含一个 imageview 和一个 textview。 有什么方法可以通过单击按钮在我当前的活动中动态添加完全相同属性的相对布局?

【问题讨论】:

  • 好的,非常感谢。

标签: android generics android-relativelayout dynamically-generated


【解决方案1】:

它会是这样的:

yourButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        ViewGroup container = (ViewGroup) v.getParent();
        LayoutInflater inflater = getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View addView = inflater.inflate(R.layout.your_id);
        container.addView(addView);
    }
});

但是,这可能无法按您的预期方式工作。要确保将视图添加到您想要的位置,请在该位置创建一个空布局,然后使用container 引用该布局并将addView 添加到该布局。祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    相关资源
    最近更新 更多