简体   繁体   中英

How do i force an embedded android fragment to a fixed width

Set i have a fragment CustomSwitchFragment, that i want to use as followed:

<fragment android:name="m6world.test_toggle_yes_no.CustomSwitchFragment"
        android:id="@+id/custom_switch_fragment"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />

i don't want to have to input 80dp, i would like to be able to put wrap_content and the fragment will be set whatever width i used when created it. Basically i don`t want to have to tell people who are gonna use my fragment, to set it to 80dp specifically.

I guess you can use something like this

    Display display = getWindow().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.width = width * 70 / 100;
    params.height = height * 30 / 100;
    getWindow().setAttributes(params);
    getWindow().getAttributes().verticalMargin = 0.2F;

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