Android 布局不是';动态添加时不显示

Android 布局不是';动态添加时不显示,android,android-layout,android-studio,Android,Android Layout,Android Studio,我试图在MainActivity(Android Studio创建的默认Android应用程序)中动态添加一个布局,但新的布局没有显示出来。 我尝试过不同的布局,但那一个在450像素的高度后被剪掉了 我对Android用户界面编程还很陌生,我觉得我在添加功能时做错了什么。下面是添加的代码(灵感来源于): 这是activity_main.xml: 下面是我想添加的布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLay

我试图在MainActivity(Android Studio创建的默认Android应用程序)中动态添加一个布局,但新的布局没有显示出来。 我尝试过不同的布局,但那一个在450像素的高度后被剪掉了

我对Android用户界面编程还很陌生,我觉得我在添加功能时做错了什么。下面是添加的代码(灵感来源于):

这是activity_main.xml:


下面是我想添加的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="188dp"
        android:layout_height="191dp"
        app:srcCompat="@mipmap/ic_launcher" />
</RelativeLayout>

如果静态添加布局(在xml中添加
),则会正确显示
测试布局

我做错了什么


(在Nexus 5X上使用Android 7.1.2)

使用此方法动态添加视图:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.linlayout);
View testlin= getLayoutInflater().inflate(R.layout.test_layout, myLayout, false); 
myLayout.addView(testlin); 

为什么不以编程方式创建Imageview并在布局中进行设置呢。不需要额外的亲属关系layout@DivyeshPatel原因有很多@Nitay try
linearLayout.addView(v)
@doomsnight刚刚尝试过,同样的问题。@DivyeshPatel我想添加的实际布局更复杂,这个只是为了测试添加函数LinearLayout myLayout=(LinearLayout)findViewById(R.id.linlayout);查看testlin=GetLayoutFlater()。充气(R.layout.test\u布局,myLayout,false);addView(testlin);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="188dp"
        android:layout_height="191dp"
        app:srcCompat="@mipmap/ic_launcher" />
</RelativeLayout>
LinearLayout myLayout = (LinearLayout)findViewById(R.id.linlayout);
View testlin= getLayoutInflater().inflate(R.layout.test_layout, myLayout, false); 
myLayout.addView(testlin); 
        ll_fovorite = (LinearLayout) view.findViewById(R.id.ll_fovorite);

        tv_no_fovorite = new TextView(getActivity());
        tv_no_fovorite.setText("There are no fovorite words");
        tv_no_fovorite.setTextColor(Color.parseColor("#8b8682"));
        tv_no_fovorite.setTextSize(24);
        tv_no_fovorite.setGravity(Gravity.CENTER);
        tv_no_fovorite.setTypeface(Typeface.MONOSPACE);
        ll_fovorite.addView(tv_no_fovorite, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);