2011-02-17 67 views
153

为了让LayoutInflater按照预期工作,我遇到了很多麻烦,其他人也这样做:How to use layoutinflator to add views at runtime?为什么LayoutInflater会忽略我指定的layout_width和layout_height布局参数?

为什么LayoutInflater不理我指定布局参数?例如。为什么我的资源XML中的layout_widthlayout_height值不符合?

+0

道道通确实是伤害还是我挑错了地方?只是想我会分享我的结果。必须承认教程没有具体提到常见问题页面。 – andig 2011-02-17 09:43:14

+3

请参阅常见问题第一个问题的最后一段。将其作为问题发布,然后将教程发布为答案。当然,你可以编辑这个问题,并在答案中粘贴教程。如果你解决这个问题,我会保持乐观。 – bigstones 2011-02-17 10:22:52

回答

352

我已经研究过此问题,指的是LayoutInflater docs和设置小样本示范项目。以下教程显示如何使用LayoutInflater动态填充布局。

在我们开始之前看看LayoutInflater.inflate()参数是这样的:

  • 资源:一个XML布局资源ID负载(例如,R.layout.main_page
  • :可选观点是所生成的分层结构的父(如果attachToRoottrue),或者简单地提供一组用于返回的层次结构的根LayoutParams值(如果是attachToRootfalse一个对象。 )
  • attachToRoot:无论充气层次结构应附加到根参数?如果为false,则root仅用于为XML中的根视图创建LayoutParams的正确子类。

  • 返回:充气层次结构的根视图。如果提供root并且attachToRoottrue,则这是根;否则它就是膨胀的XML文件的根源。

现在的样本布局和代码。

主要布局(main.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</LinearLayout> 

添加到该容器是一个独立的TextView,作为红色的小正方形可见如果布局参数被成功地从XML应用(red.xml):

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="25dp" 
    android:layout_height="25dp" 
    android:background="#ff0000" 
    android:text="red" /> 

现在LayoutInflater使用与调用几个变化参数

public class InflaterTest extends Activity { 

    private View view; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     ViewGroup parent = (ViewGroup) findViewById(R.id.container); 

     // result: layout_height=wrap_content layout_width=match_parent 
     view = LayoutInflater.from(this).inflate(R.layout.red, null); 
     parent.addView(view); 

     // result: layout_height=100 layout_width=100 
     view = LayoutInflater.from(this).inflate(R.layout.red, null); 
     parent.addView(view, 100, 100); 

     // result: layout_height=25dp layout_width=25dp 
     // view=textView due to attachRoot=false 
     view = LayoutInflater.from(this).inflate(R.layout.red, parent, false); 
     parent.addView(view); 

     // result: layout_height=25dp layout_width=25dp 
     // parent.addView not necessary as this is already done by attachRoot=true 
     // view=root due to parent supplied as hierarchy root and attachRoot=true 
     view = LayoutInflater.from(this).inflate(R.layout.red, parent, true); 
    } 
} 

参数变化的实际结果记录在代码中。

摘要:调用LayoutInflater时未指定根将导致膨胀调用,忽略XML中的布局参数。调用带有根不相等nullattachRoot=true不加载布局参数膨胀,但再次返回根对象,其防止进一步的布局改变到加载的对象(除非你可以找到它使用findViewById())。 你最有可能想使用调用约定因此这个人是:

loadedView = LayoutInflater.from(context) 
       .inflate(R.layout.layout_to_load, parent, false); 

为了帮助布局问题时,hierarchy viewer强烈推荐。

+18

优秀的答案。在Android上工作了3年,并且还在计数,并且仍然有一些可以从中学习。 – 2012-09-27 17:03:04

4

andig是正确的,LayoutInflater忽略你的layout_params的一个常见原因是因为未指定根目录。许多人认为你可以传入null作为根。这对于一些场景是可以接受的,例如对话框,在创建时您无法访问root。然而,一个好的规则是,如果你有root权限,把它交给LayoutInflater。

我写了一个深入的博客文章关于这一点,你可以看看这里:

https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/

相关问题