Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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
Java 当以编程方式而不是xml方式包含时,无法获取相同的视图_Java_Android_Xml_Android Layout_Layout - Fatal编程技术网

Java 当以编程方式而不是xml方式包含时,无法获取相同的视图

Java 当以编程方式而不是xml方式包含时,无法获取相同的视图,java,android,xml,android-layout,layout,Java,Android,Xml,Android Layout,Layout,最近,我在以编程方式包含布局方面遇到了一个问题 场景: 我有主布局和一个子布局 我想以编程方式将子布局包括到主布局中 主布局根是相对布局,子布局根是framelayout 我想添加要添加到主布局底部的子布局,因为我已将属性添加到子布局 奇怪的是,如果我静态地(在xml中使用include标记)将子布局添加到主布局中,那么它会按预期工作(一直到底),但如果以编程方式添加,则不会工作 mainlayout.xml <?xml version="1.0" encoding="utf-8"?>

最近,我在以编程方式包含布局方面遇到了一个问题

场景:

  • 我有主布局和一个子布局
  • 我想以编程方式将子布局包括到主布局中
  • 主布局根是相对布局,子布局根是
    framelayout
  • 我想添加要添加到主布局底部的子布局,因为我已将属性添加到子布局
  • 奇怪的是,如果我静态地(在xml中使用include标记)将子布局添加到主布局中,那么它会按预期工作(一直到底),但如果以编程方式添加,则不会工作
  • mainlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/ly_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".AppintroActivity">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </RelativeLayout>
    
        <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
    
        <RelativeLayout
            android:id="@+id/ly_bottom_holder"
            android:layout_width="match_parent"
            android:gravity="bottom"
            android:layout_height="@dimen/_50sdp"
            android:layout_gravity="bottom">
    
            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="@color/dull_black" />
    
            <TextView
                android:id="@+id/txt_skip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/_10sdp"
                android:padding="@dimen/_5sdp"
                android:text="Skip"
                android:textAllCaps="true"
                android:textColor="@color/white" />
    
            <ImageView
                android:id="@+id/img_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:padding="@dimen/_5sdp"
                android:src="@drawable/forward" />
    
            <LinearLayout
                android:id="@+id/dots"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:orientation="horizontal" />
    
    
        </RelativeLayout>
    
    
    </FrameLayout>
    

    因此,请给我一些建议,通过使用java代码,如何实现与使用xml完全相同的结果。

    我认为您必须使用“LayoutParams”向布局添加视图。像这样:

        RelativeLayout.LayoutParams par=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        par.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        ly_root.addView(bottomlayout,par);
    

    您必须在参数中应用
    RelativeLayout
    规则,并将其与参数一起添加,您可以执行以下操作:

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    
    ly_root.addView(bottomlayout, params);
    

    如果您在实施过程中遇到问题,请告诉我。

    完成,兄弟,谢谢您@mehd的帮助
    RelativeLayout layout = findViewById(R.id.my_relative_layout);
    
    View frame = getLayoutInflater().inflate(R.layout.sub_layout,null); 
    
    lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
    
    LayoutParams.MATCH_PARENT); // You might want to tweak these to WRAP_CONTENT
    
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    
    layout.addView(frame, lp);
    
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    
    ly_root.addView(bottomlayout, params);