Ich versuche, ein Layout in MainActivity dynamisch hinzuzufügen (Standard-Android-Anwendung, die Android Studio erstellt), aber das neue Layout wird nicht angezeigt. Ich habe es mit einem anderen Layout versucht, aber dieses wurde nach ~ 450px Höhe abgeschnitten.Layout wird nicht angezeigt, wenn dynamisch hinzugefügt
Ich bin ziemlich neu in Android UI-Programmierung und ich habe das Gefühl, ich mache etwas falsch in der Addierfunktion. Hier ist das Hinzufügen von Code (die von this answer inspiriert wurde):
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
View v = vi.inflate(R.layout.test_layout, null);
// insert into main view
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
1000,//ViewGroup.LayoutParams.FILL_PARENT,
1000);//ViewGroup.LayoutParams.FILL_PARENT);
linearLayout.addView(v, 0, layoutParams);
Dies ist activity_main.xml:
<include
android:id="@+id/include"
layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<LinearLayout
android:id="@+id/linlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!--<include layout="@layout/test_layout"/>-->
</LinearLayout>
Und hier ist das Layout ich versuche hinzuzufügen:
<?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>
Wenn ich die Layou hinzufügen t statisch (<include layout="@layout/test_layout"/>
in der XML-Datei hinzufügen), test_layout
wird ordnungsgemäß angezeigt.
Was mache ich hier falsch?
(mit Android 7.1.2 auf dem Nexus 5X)
, warum Sie nicht Imageview programmatisch und setzte in dem Layout machen. Es ist kein zusätzliches relatives Layout erforderlich. –
@DivyeshPatel Es gibt viele Gründe. @Nitay versuchen 'linearLayout.addView (v);' – Doomsknight
@Doomsknight gerade versucht, das gleiche Problem. – Nitay