android - Inflater inflate 但高度很小(看起来像 wrap_content 需要 fill_parent)

标签 android

我有布局,我想膨胀它

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

</LinearLayout>

喜欢

    LinearLayout ll=(LinearLayout)findViewById(R.id.llContainer);
    View view; 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = inflater.inflate(R.layout.question_free_text, null);
    ll.addView(view);

ll在哪里

<LinearLayout
    android:id="@+id/llContainer"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
>
</LinearLayout>

在其他 xml 中,但问题是当它膨胀时显示但高度很大(fill_parent,它看起来像 wrap_content,但布局中没有 wrap_content)。谁能帮帮我?

最佳答案

正如 Yashwanth Kumar 在评论中正确提到的那样,膨胀方法的第二个参数应该是将插入新 View 的 Root View :

LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.question_free_text, ll);

如果在 inflate-call 中提供了 Root View ,则 LayoutInflator 会调用此 Root View 的 generateLayoutParams(ViewGroup.LayoutParams p) 方法来获取一些 LayoutParams(基本上包含有关大小的信息view can/should be) 将传递给新 View 。

请注意,如果您提供 Root View ,膨胀 View 将通过 root.addView(View child, LayoutParams params) 自动添加到 Root View 。

您还可以将第三个参数传递给 inflate-method (boolean attachToRoot),如果此值为 false,则新 View 不会自动添加到 Root View ,但 LayoutParams 仍使用 setLayoutParams(params) 设置。如果您想手动将 View 添加到 Root View (例如,在特定位置/索引处),则可以使用它:

LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.question_free_text, ll, false); // the LayoutParams of view are set here
ll.addView(view, 2);

关于android - Inflater inflate 但高度很小(看起来像 wrap_content 需要 fill_parent),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7752642/

相关文章:

android - arm-linux-androideabi/bin/ld : fatal error: -soname: must take a non-empty argument

java - Android:处理 ListView 中的 CheckBox

android - 显示 AdMob 横幅时调整布局

java - 保存 WebView 的状态并重新加载位置

Android,如何清除大多数手机中可以从主页按钮获得的最近任务列表?反射(reflection)是一种可能的方式吗?

android - 营养数据谷歌适合

java - 如何通过 Java 更改 strings.xml 中的值 (Android)

android - smali文件名中是否允许使用#符号?

android - 将 MotionEvent 从 Java 发送到 JNI

android - Retrofit2:将post参数添加到拦截器中