简体   繁体   中英

Set image for ImageView on another layout

I am trying to set the background image of an ImageView on another layout. The other layout is the table row layout. Here is the code

ImageView pdfImage;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.help);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService
         (Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.listlayout, null);
    pdfImage = (ImageView)ll.findViewById(R.id.pdfImage);

    pdfImage.setBackgroundResource(R.drawable.pdflogo);

The pdflogo is not getting set to the image. It is just blank. How can I do this?

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

<RelativeLayout android:layout_width="wrap_content" android:layout_height="50dp">       
    <ImageView android:id="@+id/selfImage" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/selfhelpnavbar"/>
    <ImageView android:id="@+id/logoImage" android:layout_width="70dp" android:layout_height="46dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" />
    <ImageView android:id="@+id/mainLogo" android:layout_width="50dp" android:layout_height="46dp" android:background="@drawable/mainlogo" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/>
</RelativeLayout> 

    <ListView
        android:id="@+id/helpList"
        android:layout_width="wrap_content"
        android:layout_height="387dp" >
    </ListView>

<!-- Tab Bar -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/helpButton"
        android:layout_width="0.0dip"
        android:layout_height="fill_parent"
        android:background="@drawable/selfhelptab_selected" 
        android:layout_weight="1.0"
        android:onClick="onClick"
        android:clickable="true"/>

    <ImageButton
        android:id="@+id/eButton"
        android:layout_width="0.0dip"
        android:layout_height="fill_parent"
        android:background="@drawable/eapservicestab" 
        android:layout_weight="1.0"
        android:onClick="onClick"
        android:clickable="true"/>

    <ImageButton
        android:id="@+id/mailButton"
        android:layout_width="0.0dip"
        android:layout_height="fill_parent"
        android:background="@drawable/mailtab" 
        android:layout_weight="1.0"
        android:onClick="onClick"
        android:clickable="true"/>

    <ImageButton
        android:id="@+id/gethelpButton"
        android:layout_width="0.0dip"
        android:layout_height="fill_parent"
        android:background="@drawable/gethelptab" 
        android:layout_weight="1.0"
        android:onClick="onClick"
        android:clickable="true"/>

    <ImageButton
        android:id="@+id/moreButton"
        android:layout_width="0.0dip"
        android:layout_height="fill_parent"
        android:background="@drawable/moretab" 
        android:layout_weight="1.0"
        android:onClick="onClick"
        android:clickable="true"/>

</LinearLayout></LinearLayout>

在此处输入图片说明

setContentView(R.layout.help);

with this statement, you're setting help.xml as your layout to be shown. Later on, you inflate another layout, do something with it's image, but as far as I can see, this layout is neither part of help.xml, nor it is set to be shown via setContentView.

try this:

ImageView pdfImage;

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


    LayoutInflater inflater = (LayoutInflater) context.getSystemService
         (Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.listlayout, null);
    pdfImage = (ImageView)ll.findViewById(R.id.pdfImage);

    pdfImage.setBackgroundResource(R.drawable.pdflogo);
    setContentView(ll);

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