如何使浮动视图占据布局的整个宽度和高度?

时间:2019-07-08 14:20:01

标签: android flutter

我正在开发一个向我在Android Studio中开发的应用程序中添加抖动的应用程序。

具体来说,我有一个按钮,允许我从android传递到flutter,但是当flutter视图打开时,它并不会占用android布局的全部尺寸。

有人知道我如何使抖动视图占据android布局的全部尺寸吗?

Android layout

这是我已经拥有的代码:

public class MainActivity extends AppCompatActivity {

Button btn_flutter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn_flutter = findViewById(R.id.btn_flutter);
    btn_flutter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View flutterview = Flutter.createView(MainActivity.this,getLifecycle(),"r1");

            FrameLayout.LayoutParams framelayout =  new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            addContentView(flutterview, framelayout);
        }
    });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<Button
    android:id="@+id/btn_flutter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Flutter"
    tools:layout_editor_absoluteX="161dp"
    tools:layout_editor_absoluteY="341dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

在Flutter上使用Size size = MediaQuery.of(context).size;
尝试按照以下步骤制作Flutter Widget:

@override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          color: Colors.redAccent,
          width: size.width,
          height: size.height,
        ),
      ),
    );
  }