2012-07-24 243 views
1

我想使用帶有背景圖像的登錄窗體中的滾動視圖,但圖像延伸。Android:ScrollView延伸背景圖像

我的代碼如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:id="@+id/login_layout" > 

    <LinearLayout 
     android:layout_width="250dp" 
     android:layout_height="270dp" 
     android:layout_marginTop="40dp" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:background="@drawable/login" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="30dp" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/username_login" /> 

      <EditText 
       android:id="@+id/username_login" 
       android:inputType="text" 
       android:layout_width="200dp" 
       android:layout_height="30dp" 
       android:paddingLeft="5dp" 
       android:paddingTop="2dp" 
       android:background="@drawable/edit_text" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/password_login" /> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

</ScrollView> 

我有2個文本編輯的用戶名和密碼,登錄按鈕

android:background="@drawable/login" 

是佈局的背景圖像拿着文字編輯和按鈕

請幫忙嗎?

知道我設置背景(即延伸)活動

裏面我設置的背景活動中

private void setBackground(){ 


     LinearLayout layout = (LinearLayout) findViewById(R.id.login_layout); 
     layout.setBackgroundDrawable(application.getBackground01()); 

    } 
+0

android:background =「@ drawable/login」在你的ScrollView本身設置這個 – pixelscreen 2012-07-24 11:40:26

+0

你試過android:src而不是android:background嗎? – 2012-07-24 11:40:59

+0

我編輯我的帖子,請檢查它 – 2012-07-24 11:53:13

回答

1

你可以試試這個代碼。希望是會起作用的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/login_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/login" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="250dp" 
      android:layout_height="270dp" 
      android:layout_marginTop="40dp" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="30dp" 
       android:orientation="vertical" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/username_login" /> 

       <EditText 
        android:id="@+id/username_login" 
        android:layout_width="200dp" 
        android:layout_height="30dp" 
        android:background="@drawable/edit_text" 
        android:inputType="text" 
        android:paddingLeft="5dp" 
        android:paddingTop="2dp" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/password_login" /> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 
+0

在某些形式我有更多的我孩子的滾動視圖,它不允許我這樣做,所以我必須有滾動視爲主要的家長。 你有另一種解決方案嗎? – 2012-07-24 12:40:58

+0

如果您有多個孩子,那麼我不會收到您,然後在滾動視圖的第一個線性佈局中輸入這些孩子。 – 2012-07-24 13:46:50

1

這麼多的研究之後得到了解決:

第一步:如果您在活動試圖設置的onCreate:從.xml文件

android:background="@drawable/background" 

第二步(一)刪除下面的行作爲

protected void onCreate(Bundle savedInstanceState) { 
    .... 
    getWindow().setBackgroundDrawableResource(R.drawable.image_resourcename); 
    .... 
} 

第二步(b):如果你想在片段設置爲onCreateView

protected void onCreate(Bundle savedInstanceState) { 
    .... 
    getActivity().getWindow().setBackgroundDrawableResource(R.drawable.image_resourcename); 
    .... 
}