android - 在 Android 中以编程方式实现 splitView

标签 android android-layout android-fragments

我正在尝试以编程方式在 Android 中创建一个 splitView。这个功能对我来说是全新的。经过一番研究,我意识到必须使用 fragment 。

<?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>

我想知道,LinearLayout 是布局的最佳选择吗?其次,是否也可以通过编程方式将按钮、选择器等 UI 部件添加到 fragment 中?

最佳答案

如果您不想在不支持 fragment 的较低 android 版本中使用 fragment 布局或构建应用程序,在这种情况下,您可以通过在一个 Activity 中调用两个单独的 XML 文件来获得 Split View。只是根据左右的要求构建两个 xml。小例子

LinearLayout layoutMain = new LinearLayout(this); layoutMain.setOrientation(LinearLayout.HORIZONTAL); setContentView(layoutMain); LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate( R.layout.firstxml,空); RelativeLayout layoutRight = (RelativeLayout) inflate.inflate( R.layout.secondxml,空);

RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight, relParam);

} `

关于android - 在 Android 中以编程方式实现 splitView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8558043/

相关文章:

java - Android:可滑动 fragment "onTabSelected"

android - 带有 arrayadapter 和 arraylist 的 ListView 中的两个 TextView

android - 如何仅在最顶部的 fragment 上触发 onResume(),在后退时

android - 获取用户信息 Facebook SDK 时签名的 Android 应用程序崩溃

android - 如何在 OS 2.x 的 HTC 设备中禁用编辑文本中的剪切复制粘贴选项

android - 调用 findViewById() 获取 fragment View 时出现 NullPointerException

android - Activity 问题中的 findViewById

android - fragment 中未调用 AfterViews 方法?

android - 如何在android中改变图像的亮度?

java - 如何在 Firestore 中使用 POJO 更新所有文档中的一个字段?