Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式使用FlexboxLayout_Android_Flexbox_Android Flexboxlayout - Fatal编程技术网

Android 以编程方式使用FlexboxLayout

Android 以编程方式使用FlexboxLayout,android,flexbox,android-flexboxlayout,Android,Flexbox,Android Flexboxlayout,我在尝试使用创建简单界面时遇到了一些问题。 我在.xml文件中创建了视图,所有操作都按预期进行,但是当我尝试以编程方式创建相同的视图时,我无法设置布局\u flexBasisPercent。 我正在努力实现这样的目标: 以下是xml代码: <com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="htt

我在尝试使用创建简单界面时遇到了一些问题。 我在.xml文件中创建了视图,所有操作都按预期进行,但是当我尝试以编程方式创建相同的视图时,我无法设置
布局\u flexBasisPercent
。 我正在努力实现这样的目标:

以下是xml代码:

<com.google.android.flexbox.FlexboxLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:flexDirection="row"
    tools:context=".MainActivity">

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:flexDirection="row"
        app:layout_flexBasisPercent="30%"
        app:layout_order="2">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/lorem"/>

    </com.google.android.flexbox.FlexboxLayout>

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:flexDirection="row"
        app:layout_flexBasisPercent="70%"
        app:layout_order="1" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/random"/>

    </com.google.android.flexbox.FlexboxLayout>
有人能找到错误吗?或者我做错了什么


谢谢。

通读您的代码有点困难;我建议将其重构为更小的方法,以便更容易地进行推理。尽管如此,我看到了一些问题:

这应该是对fb.setFlexDirection()的调用

在这里,您创建了一个
LayoutParams
对象,该对象具有最初在“右”子对象
FlexboxLayout
上具有的弹性基础和顺序值,但随后您将这些
LayoutParams
设置为
TextView

此外,您无意中再次调用了
setLayoutDirection()
,而不是
setFlexDirection()

这里也有同样的问题

下面是一个工作活动,它完成了您在图像中发布的内容:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FlexboxLayout root = createRootView();
        setContentView(root);

        FlexboxLayout right = createRightFlexbox();
        TextView rightText = createTextView();
        right.addView(rightText);
        root.addView(right);

        FlexboxLayout left = createLeftFlexbox();
        ImageView leftImage = createImageView();
        left.addView(leftImage);
        root.addView(left);
    }

    private FlexboxLayout createRootView() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);

        FlexboxLayout flexbox = new FlexboxLayout(this);
        flexbox.setLayoutParams(params);
        flexbox.setFlexDirection(FlexDirection.ROW);

        return flexbox;
    }

    private FlexboxLayout createRightFlexbox() {
        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        params.setFlexBasisPercent(0.3f);
        params.setOrder(2);

        FlexboxLayout flexbox = new FlexboxLayout(this);
        flexbox.setLayoutParams(params);
        flexbox.setFlexDirection(FlexDirection.ROW);

        return flexbox;
    }

    private TextView createTextView() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);

        TextView textView = new TextView(this);
        textView.setLayoutParams(params);
        textView.setText(R.string.lorem);

        return textView;
    }

    private FlexboxLayout createLeftFlexbox() {
        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        params.setFlexBasisPercent(0.7f);
        params.setOrder(1);

        FlexboxLayout flexbox = new FlexboxLayout(this);
        flexbox.setLayoutParams(params);
        flexbox.setFlexDirection(FlexDirection.ROW);

        return flexbox;
    }

    private ImageView createImageView() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);

        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.drawable.random);

        return imageView;
    }
}

通过将代码拆分为多个方法,可以更轻松地查看应用于哪些视图的布局参数。此外,您还可以在顶层看到屏幕的整体结构。

您是否需要专门使用
FlexboxLayout
?从您发布的“所需”图像来看,这看起来像是您只需使用
线性布局即可完成的操作!是的,我需要使用FlexboxLayout。我必须创建更复杂的视图,但我必须从这样一个最简单的视图开始。非常感谢@BenP!显然,我的代码一团糟,我认为将flex基值设置为“60”而不是“0.6”是这里的主要问题。再次感谢你。
fb.setLayoutDirection(FlexDirection.ROW);
FlexboxLayout.LayoutParams lpRight =
        new FlexboxLayout.LayoutParams(
                FlexboxLayout.LayoutParams.MATCH_PARENT,
                FlexboxLayout.LayoutParams.MATCH_PARENT);
lpRight.setFlexBasisPercent(30f);
lpRight.setOrder(2);
lpRight.setLayoutDirection(FlexDirection.ROW);
// Right subregions contains a Textview
TextView tv = new TextView(this);
tv.setLayoutParams(lpRight);
FlexboxLayout.LayoutParams lpLeft =
        new FlexboxLayout.LayoutParams(
                FlexboxLayout.LayoutParams.MATCH_PARENT,
                FlexboxLayout.LayoutParams.MATCH_PARENT);
lpLeft.setFlexBasisPercent(70f);
lpLeft.setOrder(1);
lpLeft.setLayoutDirection(FlexDirection.ROW);
// Left subregion contains an Imageview
ImageView iv = new ImageView(this);
iv.setLayoutParams(lpLeft);
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FlexboxLayout root = createRootView();
        setContentView(root);

        FlexboxLayout right = createRightFlexbox();
        TextView rightText = createTextView();
        right.addView(rightText);
        root.addView(right);

        FlexboxLayout left = createLeftFlexbox();
        ImageView leftImage = createImageView();
        left.addView(leftImage);
        root.addView(left);
    }

    private FlexboxLayout createRootView() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);

        FlexboxLayout flexbox = new FlexboxLayout(this);
        flexbox.setLayoutParams(params);
        flexbox.setFlexDirection(FlexDirection.ROW);

        return flexbox;
    }

    private FlexboxLayout createRightFlexbox() {
        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        params.setFlexBasisPercent(0.3f);
        params.setOrder(2);

        FlexboxLayout flexbox = new FlexboxLayout(this);
        flexbox.setLayoutParams(params);
        flexbox.setFlexDirection(FlexDirection.ROW);

        return flexbox;
    }

    private TextView createTextView() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);

        TextView textView = new TextView(this);
        textView.setLayoutParams(params);
        textView.setText(R.string.lorem);

        return textView;
    }

    private FlexboxLayout createLeftFlexbox() {
        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        params.setFlexBasisPercent(0.7f);
        params.setOrder(1);

        FlexboxLayout flexbox = new FlexboxLayout(this);
        flexbox.setLayoutParams(params);
        flexbox.setFlexDirection(FlexDirection.ROW);

        return flexbox;
    }

    private ImageView createImageView() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);

        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.drawable.random);

        return imageView;
    }
}