2011-03-15 114 views
18

我有一個listview的典型例子,edittext和活動底部的一個按鈕。如何防止軟鍵盤調整背景圖片的大小

當我點擊edittext時,出現軟鍵盤,我可以滾動listview中的項目,但它會重新調整背景圖像的大小。

我試過android:windowSoftInputMode="adjustResize"但沒有區別。

我試過android:windowSoftInputMode="adjustPan"。圖像沒有被壓扁,整個佈局向上移動,我失去了標題欄。如果列表項目超出佈局大小,我只能滾動列表。

基本上,我想維護標題欄,保留背景圖像而不用重新調整大小,並允許滾動列表項。任何人都設法做到這一點?謝謝!

回答

3

無法阻止軟鍵盤調整背景圖像的大小。

使用此scaleType(matrix)並準備合適的圖像。

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/RelativeLayoutchat" 
    > 

    <LinearLayout 
    android:orientation="horizontal" 
    android:gravity ="clip_horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true"> 

    <ImageView 
     android:gravity ="clip_horizontal" 
     android:id="@+id/chat_bak_img" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="matrix"> 
    </ImageView> 

    </LinearLayout> 

    <LinearLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <ListView 
     android:id="@+id/chat" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:transcriptMode="alwaysScroll" 
     android:divider="#000000" 
     android:clickable="false" 
     android:layout_weight="9" 
     android:cacheColorHint="#00000000"/> 



    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     style="@android:style/ButtonBar" 
     android:gravity="center" 
     android:paddingLeft="3dip" 
     android:paddingTop="3dip" 
     android:paddingRight="3dip"> 
     <EditText 
      android:id="@+id/chatMsg" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:maxLines="5" 
      android:enabled="true" 
      android:textSize="17sp" 
      android:maxLength="150"/> 
     <Button 
      android:id="@+id/sendMsg" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/send_withSpace" 
      android:layout_weight="4"/> 
    </LinearLayout> 

    </LinearLayout> 

</RelativeLayout> 
+0

Ehm,我認爲adjustPan通常會阻止bg調整大小。 – Ted 2013-01-13 13:37:16

37

的列表視圖,您需要使用

android:isScrollContainer="false" 

,這在manifest.xml文件添加到您的活動

android:windowSoftInputMode="adjustPan" 
+6

沒有爲我工作。 – mschonaker 2011-07-21 17:19:14

+1

嘗試過Meny解決方案,最後爲SCROLL-VIEW工作。非常感謝。 – 2013-05-29 13:57:46

9

轉至AndroidManifest.xml中和:

activity name="activityname" android:windowSoftInputMode="stateVisible|adjustPan" 
+1

但是,這調整了BG圖像 – 2013-04-23 14:10:16

19

我已經嘗試過可能解決方案爲solvi這個問題。 android:windowSoftInputMode =「adjustPan」可以讓你的整個屏幕在鍵盤上移動。一般來說,我們有一個頂部的屏幕標題。使用這個標誌,它也會離開可見區域,這會造成不好的用戶體驗。我使用android:windowSoftInputMode="adjustResize"這將調整整個屏幕,但它會在問題中引起@Maurice狀態的相同問題。

因此,這裏是我的最終解決方案:

清單中

android:windowSoftInputMode="adjustResize|stateAlwaysHidden" 

以XML

不要設置任何背景here.And讓您的視圖滾動型

在Java中

您需要將背景設置爲窗口:

getWindow().setBackgroundDrawableResource(R.drawable.bg_wood) ; 
+2

+1這是正確的答案,如果你想要你的元素調整大小,而你的背景保持原樣。這個答案值得更多關注。 – Levit 2014-10-21 08:19:40

+1

太棒了!很好的答案! – gonzalomelov 2015-07-02 22:18:46

+0

真棒ans,通過這些得到解決方案.. – 2015-07-09 10:01:26

1

我有imageview的同樣的問題,調整大小,而鍵盤了,我已經解決了明顯的android:windowSoftInputMode =「adjustPan」和下面是佈局實例在我的情況下

i。Ëfrg_write_social:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/frg_writeyourpost_ll_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:fitsSystemWindows="true" 
android:isScrollContainer="false" 
android:scrollbars="none" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/frg_writeyourpost_rl_top" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/frg_writeyourpost_ll_bottum" 
     android:layout_alignParentTop="true" 
     android:minHeight="400dip" > 

     <ImageView 
      android:id="@+id/frg_writyourpost_iv" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_centerInParent="true" 
      android:adjustViewBounds="true" 
      android:scaleType="fitXY" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/frg_writeyourpost_ll_bottum" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" 
     android:padding="10dp" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="10dp" 
      android:background="@android:color/white" 
      android:gravity="center" 
      android:orientation="vertical" 
      android:padding="10dp" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:gravity="center" 
       android:text="Write your post" 
       android:textColor="#727F8D" 
       android:textSize="10sp" /> 

      <com.example.views.CustomEditText 
       android:id="@+id/frg_writeyourpost_et_message" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/slide_image" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:background="@null" 
       android:inputType="textMultiLine" 
       android:maxHeight="@dimen/slide_image" 
       android:maxLines="3" 
       android:textSize="11sp" > 
      </com.example.views.CustomEditText> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:orientation="horizontal" > 

       <TextView 
         android:id="@+id/frg_writeyourpost_tv_totalcharacter" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center" 
        android:text="0" 
        android:textColor="#727F8D" 
        android:textSize="8sp" /> 

       <TextView 
       android:id="@+id/frg_writeyourpost_tv_characterLeft" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:gravity="center" 
        android:text="character" 
        android:textColor="#727F8D" 
        android:textSize="8sp" /> 
      </LinearLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <RadioGroup 
       android:id="@+id/frg_writepost_fbtw_rggroup" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" > 

       <RadioButton 
        android:id="@+id/frg_writepost_rb_facebook" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/fb_check_selector" 
        android:button="@android:color/transparent" 
        android:gravity="center" 
        android:padding="10dip" 
        android:text="@string/post_for_facebook"/> 

       <View 
        android:layout_width="5dip" 
        android:layout_height="match_parent" 
        android:background="@android:color/transparent"/> 

       <RadioButton 
        android:id="@+id/frg_writepost_rb_twitter" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/tw_check_selector" 
        android:button="@android:color/transparent" 
        android:gravity="center" 
        android:padding="10dip" 
        android:text="@string/post_for_twitter"/> 
      </RadioGroup> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

是否有可能加載圖像動態可以用picaaso調整圖像,而負載與FILL_PARENT imageview的

1

使用的ImageView和centerCrop在RelativeLayout的屬性。喜歡這個;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/yazisma_arka_plan" 
     android:scaleType="centerCrop" 
     /> 
    <ListView 
     android:id="@+id/messages_view" 
     android:layout_width="fill_parent"... 
3

使用android:windowSoftInputMode="adjustResize"和簡單包裝你的背景的ImageView在滾動型。

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scrollbars="none" 
    android:overScrollMode="never"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</ScrollView> 
+0

<滾動型 機器人:layout_width = 「match_parent」 機器人:layout_height = 「WRAP_CONTENT」 機器人:滾動條= 「無」 機器人:overScrollMode = 「從不」> Softlion 2017-08-08 14:54:50

0

呃,讓我看看...圖片先!

View before touched View after touched 佈局文件act_login.xml。

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/login_bkgd" 
    android:orientation="vertical" 
    android:paddingLeft="25dp" 
    android:paddingRight="25dp" 
    > 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="#0fff" 
     android:scrollbars="none" 
     android:listSelector="#0fff" 
     android:divider="#0fff" 
     android:dividerHeight="0dp" 
     android:isScrollContainer="true" 
     /> 
    <!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--> 
    <!--android:isScrollContainer="true"--> 
    <!--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!--> 

    <LinearLayout 
     android:id="@+id/loginItem" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:orientation="vertical" 
      > 

      <ImageView 
       android:id="@+id/loginLogo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="35dp" 
       android:layout_marginTop="35dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/logo" 
       /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:paddingBottom="35dp" 
       > 

       <View 
        android:layout_width="0dp" 
        android:layout_height="1px" 
        android:layout_weight="1" 
        android:background="#Ffff" 
        /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="6dp" 
        android:layout_marginRight="6dp" 
        android:text="WELCOME" 
        android:textColor="#Ffff" 
        android:textSize="14sp" 
        /> 

       <View 
        android:layout_width="0dp" 
        android:layout_height="1px" 
        android:layout_weight="1" 
        android:background="#Ffff" 
        /> 
      </LinearLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:background="@drawable/login_et_long" 
       android:gravity="center" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       > 

       <TextView 
        android:layout_width="60dp" 
        android:layout_height="wrap_content" 
        android:background="@null" 
        android:text="賬號:" 
        android:textColor="#Ffff" 
        android:textSize="16sp" 
        /> 

       <EditText 
        android:id="@+id/loginUsername" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@null" 
        android:hint="請輸入手機號" 
        android:imeOptions="actionNext" 
        android:inputType="text" 
        android:singleLine="true" 
        android:textColor="#F000" 
        android:textSize="16sp" 
        /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:layout_marginBottom="45dp" 
       android:layout_marginTop="10dp" 
       android:background="@drawable/login_et_long" 
       android:gravity="center" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       > 

       <TextView 
        android:layout_width="60dp" 
        android:layout_height="wrap_content" 
        android:background="@null" 
        android:text="密碼:" 
        android:textColor="#Ffff" 
        android:textSize="16sp" 
        /> 

       <EditText 
        android:id="@+id/loginPassword" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@null" 
        android:hint="請輸入密碼" 
        android:imeOptions="actionGo" 
        android:inputType="textPassword" 
        android:singleLine="true" 
        android:textColor="#F000" 
        android:textSize="16sp" 
        /> 
      </LinearLayout> 

      <TextView 
       android:id="@+id/login" 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:background="@drawable/login_button" 
       android:gravity="center" 
       android:padding="10dp" 
       android:text="@string/str_login" 
       android:textColor="#Ffff" 
       android:textSize="19sp" 
       /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

相關的Java代碼。

EditText etUsername, etPassword; 
EditText etFocus; 
ViewGroup base; 
int mSelection; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    base=(ViewGroup)getLayoutInflater().inflate(R.layout.act_login, null); 
    setContentView(base); 
    getWindow().setBackgroundDrawable(base.getBackground()); 
    base.setBackground(null); 
    super.onCreate(savedInstanceState); 
} 

@Override 
protected void initViews() { 
    etUsername=(EditText)findViewById(R.id.loginUsername); 
    etPassword=(EditText)findViewById(R.id.loginPassword); 

    final ViewGroup item=(ViewGroup)findViewById(R.id.loginItem); 
    base.removeView(item); 
    final View[] items=new View[item.getChildCount()]; 
    for(int i=0; i<item.getChildCount(); i++) items[i]=item.getChildAt(i); 
    item.removeAllViews(); 

    ListView lv=(ListView)base.getChildAt(0); 
    lv.setAdapter(new BaseAdapter() { 
    public View getView(int i, View v, ViewGroup vg) { 
     if(etFocus!=null && getCurrentFocus()!=etFocus){ 
      etFocus.requestFocus(); 
      etFocus.setSelection(mSelection); 
      //etFocus=null; 
     } 
     return items[i]; 
    } 
    public int getCount() {return items.length;} 
    public Object getItem(int position) {return null;} 
    public long getItemId(int position) {return 0;} 
    }); 

    /*View.OnTouchListener listener=new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent e) { 
      if(e.getAction()!=MotionEvent.ACTION_UP) return false; 
      etFocus=(EditText)v; 
      mSelection=etFocus.getSelectionEnd(); 
      ZZ.z("_________________ mSelection="+mSelection); 
      return false; 
     } 
    }; 
    etUsername.setOnTouchListener(listener); 
    etPassword.setOnTouchListener(listener);*/ 

    View.OnFocusChangeListener listener1=new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus){ 
       etFocus=(EditText)v; 
       mSelection=etFocus.getSelectionStart(); 
       ZZ.z("_________________ mSelection="+mSelection); 
      } 
     } 
    }; 
    etUsername.setOnFocusChangeListener(listener1); 
    etPassword.setOnFocusChangeListener(listener1); 
} 

AndroidManifest部分。

<activity 
     android:name=".ui.activity.ActLogin" 
     android:label="@string/str_login" 
     android:theme="@style/myTheme" 
     /> 
    <!--android:windowSoftInputMode="adjustResize"--> 

樣式部分。

<style name="myTheme" parent="@android:style/Theme.Light.NoTitleBar"> 
    <item name="android:windowTranslucentStatus">true</item> 
</style> 
0

我讀了很多的答案過來,讓拍了一些由多到讓我的應用程序的工作原理:

  1. 沒有ScrollView都在我的activity_main.xml
  2. AndroidManifest.xml增加該屬性的

    <activity android:name=".MainActivity"

標籤:

`android:windowSoftInputMode="adjustResize"` 
  • MainActivity.javaonCreate() 我加getWindow().setBackgroundDrawableResource(R.drawable.app_bg);
  • 它爲我當所有其他完整的答案我已經試過沒「T。 如果你到了這裏,我希望它也適合你。