簡體   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