我正在尝试将2个视图放在顶部,将2个视图放在底部,但是不起作用

时间:2018-08-22 21:53:19

标签: android android-layout

我想要类似的东西

  • 线性布局-垂直
  • 线性布局-水平
  • 框架布局
  • 框架布局
  • 线性布局-水平
  • 框架布局
  • 框架布局

view1 | view2

view3 | view4

我该怎么解决此代码

代码

        public class MainActivity extends AppCompatActivity {

public static LinearLayout padrao;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    padrao = findViewById(R.id.padrao);
    videoUnico();
}

@SuppressLint("ResourceType")
private void videoUnico() {
    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

    padrao.removeAllViews();

    FrameLayout view = new FrameLayout(this);
    view.setBackgroundColor(Color.BLACK);
    view.setLayoutParams(ll);
    ll.setMargins(16, 16, 16, 16);
    padrao.addView(view, ll);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            viewDois();
        }
    });

}

private void viewDois() {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, .5f);


    params.setMargins(16, 16, 16, 16);
    padrao.removeAllViews();

    FrameLayout view = new FrameLayout(this);
    view.setBackgroundColor(Color.BLACK);

    padrao.addView(view, params);

    FrameLayout view2 = new FrameLayout(this);
    view2.setBackgroundColor(Color.RED);

    padrao.addView(view2, params);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            viewTres();
        }
    });
}

private void viewTres() {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, .5f);
    padrao.removeAllViews();
    params.setMargins(16, 16, 16, 16);

    FrameLayout view = new FrameLayout(this);
    view.setBackgroundColor(Color.BLACK);

    padrao.addView(view, params);

    FrameLayout view2 = new FrameLayout(this);
    view2.setBackgroundColor(Color.RED);

    padrao.addView(view2, params);

    FrameLayout view3 = new FrameLayout(this);
    view3.setBackgroundColor(Color.GREEN);

    padrao.addView(view3, params);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            viewQuatro();
        }
    });

}

private void viewQuatro() {

    padrao.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));



    LinearLayout linearHorizonteBaixo = new LinearLayout(this);
    linearHorizonteBaixo.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,.5f));
    linearHorizonteBaixo.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout linearHorizonteCima = new LinearLayout(this);
    linearHorizonteCima.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,.5f));
    linearHorizonteCima.setOrientation(LinearLayout.HORIZONTAL);

    FrameLayout view = new FrameLayout(this);
    view.setBackgroundColor(Color.BLACK);


    FrameLayout view2 = new FrameLayout(this);
    view2.setBackgroundColor(Color.RED);

    linearHorizonteCima.addView(view);
    linearHorizonteCima.addView(view2);

    FrameLayout view3 = new FrameLayout(this);
    view3.setBackgroundColor(Color.GREEN);

    FrameLayout view4 = new FrameLayout(this);
    view4.setBackgroundColor(Color.BLUE);

    linearHorizonteBaixo.addView(view3);
    linearHorizonteBaixo.addView(view4);

    padrao.addView(linearHorizonteCima);
    padrao.addView(linearHorizonteBaixo);
   }

}

Xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/padrao"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

这是整个班级 我正在使用方法来调用其他布局 但是我的问题是当我试图在一个内部创建两个线性布局时 并得到这个错误 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

1 个答案:

答案 0 :(得分:0)

此代码为XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:layout_alignParentTop="true"
    android:weightSum="2">


    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="View2" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="View1" />

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:weightSum="2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="View4" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="View3" />

</LinearLayout>
</RelativeLayout>