Android Adapter for Huawei Spinach Source set up a virtual key

Source: Internet
Author: User

In doing spinach source building dsluntan.com vx:17061863513 Horizontal screen display, found on-line matching virtual key code did not do the cross-screen state of adaptation, resulting in a horizontal screen state, the bottom virtual key mask the layout content.

Therefore, it is also necessary to fit the Huawei virtual key in the horizontal screen state. You only need to re-request the layout when the content layout changes, while recording the currently available view width. Here's the code:

Import Android.content.Context;
Import android.content.res.Resources;
Import Android.graphics.Rect;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.ViewTreeObserver;

Import Java.lang.reflect.Method;

public class Navigationbarutil {

private static Navigationbarutil instance;public static Navigationbarutil getinstance () {return Instance = new Navigat Ionbarutil ();}    public void initactivity (View content) {mobserved = content; Add a global layout listener to the view changes in the Listener View Mobserved.getviewtreeobserver (). Addongloballayoutlistener (New        Viewtreeobserver.ongloballayoutlistener () {public void Ongloballayout () {resetviewheight ();    }    }); Layoutparams = Mobserved.getlayoutparams ();} Private View mobserved;//The available height before the private int usableheightview;//view changes, private int usablewidthview;// The available width before the view changes private Viewgroup.layoutparams layoutparams;private navigationbarutil () {}/** * Resets the height of the view so that it is not obscured by the bottom virtual key */    private void Resetviewheight () {int usableheightviewnow = Calculateavailableheight ();    int usablewidthviewnow = Calculateavailablewidth (); Compare the available heights of the view before and after the layout change if (usableheightviewnow! = Usableheightview) {//If two times the height is inconsistent//to set the current view's available height to the view's actual Height layoutparams.height = UsableheightviewNow;    Mobserved.requestlayout ();//Request re-layout usableheightview = Usableheightviewnow;        } if (Usablewidthviewnow! = Usablewidthview) {layoutparams.width = Usablewidthviewnow;    Mobserved.requestlayout ();//Request re-layout usablewidthview = Usablewidthviewnow;    }}/** * Calculates the width of the view * @return */private int calculateavailablewidth () {rect r = new Rect ();    Mobserved.getwindowvisibledisplayframe (R); return (r.right);}    /** * Calculate the height of the view * * @return */private int calculateavailableheight () {rect r = new Rect (); Mobserved.getwindowvisibledisplayframe (R);

return (r.bottom-r.top);//If it is not the immersion status bar, you need to subtract the top height
return (r.bottom);//If the Immersion status bar
}

/** * 判断底部是否有虚拟键 * * @param context * @return */public boolean hasNavigationBar(Context context) {    boolean hasNavigationBar = false;    Resources rs = context.getResources();    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");    if (id > 0) {        hasNavigationBar = rs.getBoolean(id);    }    try {        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");        Method m = systemPropertiesClass.getMethod("get", String.class);        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");        if ("1".equals(navBarOverride)) {            hasNavigationBar = false;        } else if ("0".equals(navBarOverride)) {            hasNavigationBar = true;        }    } catch (Exception e) {    }    return hasNavigationBar;}

}
Invocation method: Called after the Setcontentview () method in the extracted activity base class

    //适配华为部分机型虚拟键    if (NavigationBarUtil.getInstance().hasNavigationBar(this)) {        NavigationBarUtil.getInstance().initActivity(findViewById(android.R.id.content));    }

Android Adapter for Huawei Spinach Source set up a virtual key

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.