繁体   English   中英

如何在linearlayout中动态创建框架布局并使用其他xml文件

[英]How to create dynamically framelayout in linearlayout and using other xml file

我搜索了一会儿,没有找到遮阳篷,我必须说我在创建应用程序方面非常新

我从sqlite数据库中读取了类别(名称,说明)(我不知道该数据库有多少个类别),

现在我创建一个包含linearlayout(horizo​​ntal)的水平滑块。

在此linearlayout中,我将动态创建framelayout(包含其他框架布局,linearlayouts等类似的自定义xml代码。)

我不知道如何插入xml ...

我希望有人可以帮助我找到解决这个问题的方法

-编辑-

主要活动

public class MainActivity extends Activity {

int scr_w, scr_h;

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

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    scr_w = size.x;
    scr_h = size.y;

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.menu_box_layout, null, false);

    LinearLayout llTest = (LinearLayout)findViewById(R.id.horilila);
    //Create the Menu Boxes here
    for(int i =1;i<=6; i++){

        FrameLayout flTest = new FrameLayout(this);
        flTest.addView(v, scr_w, LayoutParams.MATCH_PARENT);

        flTest.setId(i + 10);
        //Insert the new FrameLayout into the LinearLayout
        llTest.addView(flTest);

    }
}

主要xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<FrameLayout
    android:id="@+id/mailLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/horilila"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
        </LinearLayout>

    </HorizontalScrollView>

</FrameLayout>

菜单框的布局,我将插入:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

    <FrameLayout

        android:layout_height="match_parent"
        android:background="#fab3ff" >

        <TextView 
            android:text="This is a test..." />

    </FrameLayout>


</RelativeLayout>

假设您有一个xml,可以像这样从中获取视图:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.<your_xml>, null, false);

然后,您可以将此视图添加到LinearLayout中:

linearLayout.addView(v);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM