简体   繁体   中英

Android - can't change RelativeLayout width/height dynamically

I'm using MediaPlayer with this xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView">

</WebView>
<RelativeLayout
        android:id="@+id/relativeL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">
    <SurfaceView
        android:id="@+id/surfView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

What I'm trying to do is change the width/height of relativeL (and so the player inside it) when I press a key

I'm calling this function on keypress but it doesn't have any affect, no errors, just nothing is happening. This function was working just fine before I added a webview in my xml. As you can see webview is in backround of player. Here is the function:

public static boolean makePlayerSmall(){
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(650, 400);
        params1.setMargins(100, 120, 0, 0);
        MainActivity.RL.setLayoutParams(params1);
        MainActivity.RL.invalidate();
        return true;
    }

Also here is my onCreate method in MainActivity

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setImmersiveFullScreen();
        setContentView(R.layout.activity_main);
        RL = (RelativeLayout)findViewById(R.id.relativeL);

        webView = (WebView)findViewById(R.id.webView);
        assert webView != null;
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        webView.addJavascriptInterface(new MyJSInterface(this), "MyJSInterface");
        webView.loadUrl("http://85.118.98.251/misho_android/c/?debug&debug_key=4581f079f3629a2a78909251fb402a99&mac=00%3A1A%3A79%3A04%3A97%3A3E");

        //setup ui elements
        vidSurface = (SurfaceView) findViewById(R.id.surfView);
        vidHolder = vidSurface.getHolder();
        vidHolder.addCallback(this);
    }

Any help will be greatly appreciated, thanks!

   public static void replace(View target, ViewGroup.LayoutParams params) {
    if (target == null) {
        return;
    }
    final ViewParent viewParent = target.getParent();

    if (viewParent != null && viewParent instanceof ViewGroup) {
        final ViewGroup parent = (ViewGroup) viewParent;

        final int index = parent.indexOfChild(target);
        parent.removeViewInLayout(target);

        parent.addView(target, index, params);

    } else {
        throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
    }
}

Try this replacer

I tried so many things and finally it works by running the code in main thread (but i was thinking it was running in main thread in first place). I'm going to do a bit researching about this topic now.

(new Handler(Looper.getMainLooper())).post(new Runnable() {
                    @Override
                    public void run() {
                       Player.makePlayerSmall();
                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM