如何在 Android 中从 XML 膨胀视图? [英] How to Inflate view from XML in Android?

查看:26
本文介绍了如何在 Android 中从 XML 膨胀视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 tableLayout [以 XML 格式给出]

I'm creating a tableLayout [given in XML]

添加表格行[用XML创建并用Java膨胀]

adding table Row [created in XML and inflating in Java]

还将 2 个 textview 添加到表格 Row [用 XML 创建并在 JAVA 中充气]

also adding 2 textview to the table Row [created in XML and inflating in JAVA]

我只能获取背景和文本颜色,而不能获取宽度、高度和边距等布局属性来获取表格视图.

I'm able to get only the background and textcolors but not the layout properties like width, height and margin to get table view.

推荐答案

  1. 首先声明你的充气机.

  1. First declare your inflater.

LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);

  • 确定并扩展您要在当前视图上投影的新视图.

  • Identify and inflate the new view you seek to project on the current view.

    View view = inflater.inflate(R.layout.new_layout,null);
    

  • 您可能希望将新的膨胀视图添加到您的布局中.

  • You would want to add your new inflated view to your layout.

    main.addView(view);
    

  • 您可以在此处参考其他信息:http://developer.android.com/reference/android/view/LayoutInflater.html

    You can reference additional information here: http://developer.android.com/reference/android/view/LayoutInflater.html

    2019 年 5 月更新 (Kotlin):这就是在 Kotlin 中从 XML 扩展视图的方式.这是指一项活动.

    Update May 2019 (Kotlin): This is how you would inflate a view from XML in Kotlin. this is referring to an activity.

    val view = this.layoutInflater.inflate(R.layout.dialog_upgrade, null)
    mainLayout.addView(view)
    

    这篇关于如何在 Android 中从 XML 膨胀视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆