メインのxml linearlayoutに異なる背景画像を設定したいアプリケーションを作成しています.sdカードに5つの画像ファイルを保存しました.今、写真を選択して私のmaim xml linearlayoutの背景として設定したいので、それを置き換えます前の画像を表示し、新しい画像を背景として表示します。
7869 次
1 に答える
17
まず、メインの xml linearlayout に ID を割り当てます。たとえば、次の場合は「container」という名前です。
<!-- main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
次に、.java コードでレイアウト オブジェクトを見つけて、その背景としてドローアブルを設定できます。
package org.example.app;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String pathName = "/sdcard/gif001.gif";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
}
}
よろしく
陳子騰
于 2011-06-07T09:29:39.477 に答える