2013-03-17 89 views
0

我想簡單地在設置頂級中添加新的標題樣式行。不是一個圖標,就像部分文本左側的其他條目一樣,而是一個屏幕寬度的圖像。這將在其他地方實施,但現在,我只想要能夠顯示圖像在Setting.apk中顯示靜態圖像作爲標題

這似乎並不容易,我還沒有找到一個完整的指南,這個簡單的任何地方。

爲此,我將同時需要XML編輯和與之匹配的Java對象。

這裏是我當前的Java部分:

setImageDrawable(R.id.logo); 

這是我的XML方面:

<ImageView 
     android:id="@+id/logo" 
     android:src="@drawable/romlogo" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" /> 

我的形象是目前/res/drawable-xhdpi/,並命名爲"romlogo.png" (461 x 113)

因爲它位於,它甚至沒有編譯。不知道我是否錯過了import或什麼。

回答

0

您必須將Drawable解析爲setImageDrawable(...)方法。但是,XML代碼看起來不錯。不要以編程方式設置圖像,它已經在屬性android:src="@drawable/romlogo"中聲明。如果你想用代碼使用這樣設置:

// fetch the imageview and afterwards set the drawable 
// here consider the difference between R.id and R.drawable 
ImageView iv = (ImageView) findViewById(R.id.logo); 
iv.setImageDrawable(R.drawable.romlogo); 
0
<RelativeLayout 
android:id="@+id/header" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:background="#000" 
android:gravity="center" > 
<ImageView 
android:layout_width="match_parent" 
android:layout_height="200dp" 
android:paddingTop="0.1dp" 
android:paddingLeft="0.1dp" 
android:paddingRight="0,1dp" 
android:scaleType="fitXY" 
android:id="@+id/iv" 
android:src="@drawable/organic" 
android:layout_weight="30"/> 
</RelativeLayout> 

這將是適當的輸出的XML代碼。 Image as an header and of screen width

+0

如果你想保持靜態圖像,不需要任何JAVA代碼。 – 2016-08-17 12:03:15