Android Fragment 不显示在大布局上

标签 android android-layout android-fragments xamarin xamarin.android

我使用 Fragments 创建了一个应用程序,在左侧显示列表,在右侧显示详细信息。

以下是我在 layout-large Appointments.xaml 中的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="horizontal"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
  <include
        android:id="@+id/cust_toolbar"
        layout="@layout/tool_bar" />
  <fragment 
    class="com.myapp.blue.Fragments.ListFragment"
    android:id="@+id/frg_list"
    android:layout_weight="1"
    android:layout_width="0px"
    android:layout_height="match_parent" />
  <FrameLayout
    android:id="@+id/frg_detail"
    android:layout_weight="1"
    android:layout_width="0px"
    android:layout_height="match_parent" />
</LinearLayout>

这是布局文件夹中的 Appointments.xaml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#FFFFFFFF"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        android:id="@+id/cust_toolbar"
        layout="@layout/tool_bar" />
    <fragment
        class="com.myapp.blue.Fragments.ListFragment"
        android:id="@+id/frg_list"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

这是我的 Activity 代码

public class AppointmentActivity : Activity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {        
        base.OnCreate(savedInstanceState);
        //Set Content
        SetContentView(Resource.Layout.Appointments);        
        Toolbar toolbar = FindViewById<Toolbar>(Resource.Id.cust_toolbar);
        //Set Action bar
        SetActionBar(toolbar);
    }
}

该应用程序在手机上运行完美,但当我在平板电脑上运行该应用程序时,我的 fragment 都没有显示。我按照关于此 Xamarin walkthrough 的说明进行操作并且看过其他 stackoverflow 帖子,他们说宽度应该是 0px 或 dp。但是我无法让列表 fragment 显示在我的平板电脑上。我在这里缺少什么?

最佳答案

问题是您包含的工具栏很可能具有 layout_width=match_parent,因此占据了整个水平方向的线性布局。如果你想让你的工具栏在顶部,只需让你的线性布局垂直,然后在你的 fragment 中插入另一个水平线性布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
  <include
        android:id="@+id/cust_toolbar"
        layout="@layout/tool_bar" />
  <LinearLayout 
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1">

      <fragment 
        class="com.myapp.blue.Fragments.ListFragment"
        android:id="@+id/frg_list"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
      <FrameLayout
        android:id="@+id/frg_detail"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

关于Android Fragment 不显示在大布局上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686874/

相关文章:

java - 在 fragment 被分离然后重新附加后, fragment onResume 不会被调用

java - 由于 IllegalState 异常,Android TestRunner 失败

java - Facebook 应用程序的按钮链接

android - onclick 按钮在 ListView 中不起作用

android - 谁能给我解释一下 "app:layout_behavior="@string/appbar_scrolling_view_behavior”?

android - 如何在单个选项卡中显示新 fragment ?

android - 在 CoordinatorLayout Android 的 ViewPager fragment 中使用 NestedScrollView 突然滚动

android - 如何在运行每个测试用例时卸载并重新安装应用程序

android - Unity原生OpenGL贴图显示四次

android - 在 RecyclerView 中未正确解析暗模式颜色