0
我正面臨一個問題。在一個活動中,我有兩個線性佈局。第一個人的身高是300dp,第二個人的身高是150dp。我從1024 X 615資源圖像創建一個BitmapDrawable。但問題是,當我使用BitmapDrawable作爲背景時,通過setBackground()方法,BitmapDrawable在兩個佈局中顯示相同的長度,其中第一個佈局背景圖像的長度應該從第二個佈局延長兩倍。佈局之間有一些空間,但圖像長度相同。android View.setBackground()如何工作?
這裏是xml代碼。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="300dp"
android:id="@+id/rel1"
android:orientation="vertical"
>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Divider"
android:id="@+id/textView"
android:gravity="center_horizontal"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="@+id/ln2"
>
</LinearLayout>
代碼中的onCreate()
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.section_background);
BitmapDrawable backGroundBitmap = new BitmapDrawable(getResources(), background);
BitmapDrawable backGroundBitmap2 = new BitmapDrawable(getResources(), background);
findViewById(R.id.ln2).setBackground(backGroundBitmap);
findViewById(R.id.rel1).setBackground(backGroundBitmap);
如果我聲明的背景xml中的資源輸出爲ex反恐執行局。如果我在2佈局中分別使用「backGroundBitmap」和「backGroundBitmap2」,則輸出如預期的那樣。
findViewById(R.id.ln2).setBackground(backGroundBitmap);
findViewById(R.id.rel1).setBackground(backGroundBitmap2);
任何人都可以解釋爲什麼會發生這種情況。提前致謝。