在另一个布局上为ImageView设置图像

时间:2012-07-12 13:38:24

标签: android drawable android-imageview

我正在尝试设置ImageView的背景图像  在另一个布局上。另一种布局是表格行布局。这是代码

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);

pdflogo未设置为图像。它只是空白。我怎么能这样做?

<?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>

enter image description here

1 个答案:

答案 0 :(得分:1)

setContentView(R.layout.help);

使用此语句,您将设置help.xml作为要显示的布局。稍后,你膨胀另一个布局,用它的图像做一些事情,但据我所知,这个布局既不是help.xml的一部分,也不是设置为通过setContentView显示。

试试这个:

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);