简体   繁体   中英

Linear Layout with imageviews doesn't appear after inflating

I have a linear layout that I would like to show in an activity. Here's the code that I use for doing that:

final LayoutInflater factory = getLayoutInflater();
final View ps = factory.inflate(R.layout.photo_shooting, null);
final RelativeLayout photoshooting = (RelativeLayout) ps;

//other code...

rscroll.addView(photoshooting, lp);

and the layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/photoshooting"
android:background="@drawable/dropdownborder">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/selectedphoto" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/camera_and_film">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:id="@+id/camera"
        android:background="@mipmap/camera" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:id="@+id/cameraroll"
        android:background="@mipmap/cameraroll" />
</RelativeLayout>

The problem is that it doesn't appear when I add it to the relative layout rscroll. Why can't I see it when I run?

可能是因为您的RelativeLayout的ID为“ @ + id / photoshooting”,但您正在向代码中添加“ R.layout.photo_shooting”。

My problem was that I wasn't casting to the correct layout like Blackbelt mentioned. It should be:

LinearLayout photoshooting = (LinearLayout) ps;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM