我搜索了一段时间,没有找到 awnser,我必须说我在创建应用程序方面非常新手
我从sqlite数据库中读取类别(名称,描述)(我不知道这个数据库有多少个类别),
现在我创建一个包含线性布局(水平)的水平 slider 。
在此线性布局中,我将创建动态框架布局(包含自定义 xml 代码,如其他框架布局、线性布局等。)
我不知道如何插入 xml ...
我希望有人能帮我找到解决这个问题的方法
--- 编辑 ---
主要 Activity
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,您可以从中获取 View ,如下所示:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.<your_xml>, null, false);
然后您可以将此 View 添加到您的 LinearLayout 中:
linearLayout.addView(v);
关于java - 如何在线性布局中动态创建框架布局并使用其他xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21706450/