在Android中以编程方式实现splitView
我试图以编程方式在Android中创建一个splitView。这个功能对我来说是全新的。经过一些研究,我意识到必须使用碎片在Android中以编程方式实现splitView,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我试图以编程方式在Android中创建一个splitView。这个功能对我来说是全新的。经过一些研究,我意识到必须使用碎片 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="matc
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
我想知道,线性布局是布局的最佳选择吗?其次,是否可以通过编程方式将按钮、选择器等UI部件添加到片段上?您可以对片段使用任何布局,这完全取决于您的需求 是的,您可以通过编程方式将按钮等视图添加到片段中。将片段的布局声明为布局 以编程方式添加视图的一个示例--R.layout.main可以是没有子视图的线性布局
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup mViewGroup = inflater.inflate(R.layout.main, container, false);
Button mButton = new Button(getActivity());
mButton.setLayoutParameters(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
mViewGroup.add(mButton);
}
您可以使用任何带有片段的布局,这完全取决于您的需求 是的,您可以通过编程方式将按钮等视图添加到片段中。将片段的布局声明为布局 以编程方式添加视图的一个示例--R.layout.main可以是没有子视图的线性布局
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup mViewGroup = inflater.inflate(R.layout.main, container, false);
Button mButton = new Button(getActivity());
mButton.setLayoutParameters(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
mViewGroup.add(mButton);
}
如果您不想在不支持片段的较低android版本中使用片段布局或构建应用程序,那么在这种情况下,您可以通过在一个活动中调用两个单独的XML文件来获得拆分视图。只需根据需求在左右两侧构建两个xml即可。小例子
LinearLayout layoutMain=新的LinearLayout(此);
layoutMain.setOrientation(线性布局.水平);
setContentView(layoutMain);
LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u服务);
RelativeLayout layoutLeft=(RelativeLayout)充气。充气(
R.layout.firstxml,null);
RelativeLayout layoutRight=(RelativeLayout)充气。充气(
R.layout.secondxml,null);
}
`如果您不想在不支持片段的较低android版本中使用片段布局或构建应用程序,在这种情况下,您可以通过在一个活动中调用两个单独的XML文件来获得拆分视图。只需根据需求在左右两侧构建两个xml即可。小例子
LinearLayout layoutMain=新的LinearLayout(此);
layoutMain.setOrientation(线性布局.水平);
setContentView(layoutMain);
LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u服务);
RelativeLayout layoutLeft=(RelativeLayout)充气。充气(
R.layout.firstxml,null);
RelativeLayout layoutRight=(RelativeLayout)充气。充气(
R.layout.secondxml,null);
}
`谢谢Rajdeep,关于添加视图,您能提供一个示例吗?谢谢,谢谢,不过我的意思是将视图添加到片段中。您的示例似乎是将视图添加到布局中。请告知。每个片段都有自己的布局。在这种情况下,碎片布局可以是R.layout.main,关于添加视图,您能提供一个示例吗?谢谢,谢谢,不过我的意思是将视图添加到片段中。您的示例似乎是将视图添加到布局中。请告知。每个片段都有自己的布局。在这种情况下,片段布局可以是R.layout.main