动态调整片段android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10699464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:39:48  来源:igfitidea点击:

dynamic resize fragment android

androidandroid-fragments

提问by bill23

I want to change the width of a fragment I'm using in my MaiActivity? But I can't find its LayoutParamsfor changing its default width:

我想更改我在 MaiActivity 中使用的片段的宽度?但我找不到它LayoutParams来更改其默认宽度:

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int widthFfragment = 300;

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        widthTot = size.x;
        heightTot = size.y;

        findViewById(R.id.f1).getLayoutParams().width = widthTot-widthFfragment;
    }
}

This is my fragment's code:

这是我的片段代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/f1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#330000"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Fragment1.java

片段1.java

public class Fragment1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        //---Inflate the layout for this fragment---
        return inflater.inflate( R.layout.fragment1, container, false);
    }
}

Many thanks.

非常感谢。

回答by Miguel

You can do it by working with the LayoutParams instance of the View obtained from Fragment.getView(). My sample code:

您可以通过使用从 Fragment.getView() 获得的 View 的 LayoutParams 实例来实现。我的示例代码:

private void resizeFragment(Fragment f, int newWidth, int newHeight) {
    if (f != null) {
        View view = f.getView();
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(newWidth, newHeight);
        view.setLayoutParams(p);
        view.requestLayout();       


    }
}

And may be used as follows:

并且可以如下使用:

resizeFragment(myFragment, 200, 500);
resizeFragment(otherFragment, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

回答by Ian Warwick

Your right, the Fragment does not have LayoutParams because it is a conceptual container, you either need to adjust the layout params of the inflated view in the fragment, or to the container you attach the fragment to.

您的权利, Fragment 没有 LayoutParams 因为它是一个概念容器,您要么需要调整片段中膨胀视图的布局参数,要么调整片段附加到的容器。

public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ViewGroup view = (ViewGroup) inflater.inflate( R.layout.fragment1, container, false);

    // TODO Adjust layout params of inflated view here

    return view;
}

The example assumes you are inflating a ViewGroup.

该示例假设您正在膨胀一个 ViewGroup。

Hope that helps.

希望有帮助。