0

我正在尝试在另一个布局上设置 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>

在此处输入图像描述

4

1 回答 1

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);
于 2012-07-12T13:41:55.883 回答