2016-09-20 37 views
0

集我有一个片段CustomSwitchFragment,我想用如下:如何强制嵌入机器人片段设置为固定宽度

<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" /> 

我不希望有输入80dp,我会喜欢能够把wrap_content和片段将被设置为我创建它时使用的任何宽度。 基本上我不想告诉那些要使用我的片段的人,将它设置为80dp。

回答

0

我想你可以使用类似这样

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; 
相关问题