Status Bar integration and its soft-keyboard adaptive problems

Source: Internet
Author: User

Status Bar integration and its soft-keyboard adaptive problems

Status Bar integration and its soft-keyboard adaptive problems
As a beginner in android, I have never been familiar with the status bar integration problem as required by the project. The first reaction was online search. I had to say that the network really helped me a lot, but it was not enough or detailed enough for me like Xiao Bai.
After several twists and turns, the effect is finally achieved. The code is not professional and unclear, but it solves the problem I encountered. Here I will record my learning achievements and provide some help to people who need them like me.

First, status bar integration is available in Versions later than android4.4.

First:

Implementation:
First: Custom title bar
Layout: activity_main.xml


      
   
   
  
 

Custom title bar title_layout.xml


      
           
        
       
   
  

Activity implementation: MainActivity. java

Import android. app. activity; import android. content. intent; import android. OS. build; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. view. window; import android. view. windowManager; import android. widget. relativeLayout; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); RelativeLayout rlTitle = (RelativeLayout) findViewById (R. id. title); // if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.KITKAT) {Window win = getWindow (); WindowManager. layoutParams winParams = win. getAttributes (); final int bits = WindowManager. layoutParams. FLAG_TRANSLUCENT_STATUS; winParams. flags | = bits; win. setAttributes (winParams); ViewGroup. layoutParams params = rlTitle. getLayoutParams (); params. height = params. height + getStatusBarHeight (); rlTitle. setLayoutParams (params);} findViewById (R. id. button ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {startActivity (new Intent (MainActivity. this, NewActivity. class);}/*** get the height of the status bar * @ return */private int getStatusBarHeight () {int result = 0; int resourceId = getResources (). getIdentifier ("status_bar_height", "dimen", "android"); if (resourceId> 0) {result = getResources (). getDimensionPixelSize (resourceId);} return result ;}}

Isn't it easy ~~
Then integrate the second image.
Layout activity_new.xml


      
   
  

NewActivity. java

Public class NewActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_new); // if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.KITKAT) {Window win = getWindow (); WindowManager. layoutParams winParams = win. getAttributes (); final int bits = WindowManager. layoutParams. FLAG_TRANSLUCENT_STATUS; winParams. flags | = bits; win. setAttributes (winParams );}}}

OK. Next, let's take a look at some of the problems I encountered during the exploration:
1.// Version 4.4 or later
If (Build. VERSION. SDK_INT> = Build. VERSION_CODES.KITKAT ){
Window win = getWindow ();
WindowManager. LayoutParams winParams = win. getAttributes ();
Final int bits = WindowManager. LayoutParams. FLAG_TRANSLUCENT_STATUS;
WinParams. flags | = bits;
Win. setAttributes (winParams );
}

This code is used to make the status bar transparent.
2.android:fitsSystemWindows="true"
android:clipToPadding="true"

FitSystemWindows attributes:
Official description:

Boolean internal attribute to adjust view layout based on system windows such as the status bar. if true, adjusts the padding of this view to leave space for the system windows. will only take effect if this view is in a non-embedded activity. when the status bar is transparent or translucent (more than 4.4), the system sets the paddingTop value of the window view to a suitable value (the height of the status bar) make the view content not pulled to the status bar. When the status bar is not occupied (less than 4.4), The paddingTop value is set to 0 (because the status bar is not occupied Space ).

3.ViewGroup.LayoutParams params = rlTitle.getLayoutParams();
params.height = params.height + getStatusBarHeight();
rlTitle.setLayoutParams(params);

Adjust the height of the title bar, and add the height of the status bar to the original height.

Question:
The above three sections of code play a role in the specific implementation. If there is only one, this effect will appear.

The status bar disappears and seems to be integrated. However, the effect is that the title bar is moved up as a whole, occupying the position of the status bar. Text images overlap with the status bar, which is obviously not what we want.
This is the addition of code segment 2nd. In the layout file,Note:This code is added to the layout of the control you want to integrate, just as we want the color of the status bar to be the same as the color of the custom title bar title_layout, add code 2 to the root layout of title_layout:

It is better than the previous figure. At least the text is not raised to the status bar, but the height of the title bar is not .. Yes .. ... How can we do this? This is not what we want... So we can adjust the height of the title bar, that is, add code 3 and the original height of the title bar to the height of the status bar. Then OK

Look, this is the final effect, perfect implementation ....

Then there is an image problem. From the above questions, I think we can see that the first step of the results can be used to solve the image integration problem. Right, that is, when adding images, we only need one step, add the above Code 1 to the Code, and the layout file does not need to be changed at all ..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.