【问题标题】:Layout below Fragment not visible片段下方的布局不可见
【发布时间】:2013-12-11 14:39:21
【问题描述】:

我有以下布局:

<LinearLayout 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:orientation="vertical" >

<View
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>


<LinearLayout
    android:id="@+id/bottomNav"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username: " />
</LinearLayout>

我用我的片段替换视图“内容”:

    View view = inflater.inflate(R.layout.kwiz_game, container, false);
    contentView = view.findViewById(R.id.content);
    currentFragment = new KwizCardBackFragment(android.R.drawable.btn_plus);
    getFragmentManager().beginTransaction().replace(R.id.content,
            currentFragment);

但是片段占用了所有空间,并且没有显示带有 TextView 的布局。

我还尝试将 RelativeLayouts 与 alignParentBottom=true 和其他东西一起使用...

替换内容的片段视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_kwiz_item" >

 <ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:scaleType="centerCrop" />
</RelativeLayout>

片段的OnCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_card_back, container,
            false);
    ImageView imageView = (ImageView) view.findViewById(R.id.image);
    if (drawable != null) {
        imageView.setImageDrawable(drawable);
    } else {
        imageView.setBackgroundResource(drawableId);
    }
    return view;
}

经过数小时的调试后,我终于发现了错误……我的一些布局具有 ID 为“内容”的视图。我想当我寻找 R.id.content 时,会使用“最近”元素......事实并非如此!

谢谢大家!

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    我稍微修改了你的布局,这似乎达到了你想要的效果:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <FrameLayout 
            android:id="@+id/content"
            android:layout_width="match_parent" 
            android:layout_height="0dp"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/bottomNav" />
    
        <LinearLayout
            android:id="@+id/bottomNav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Username: " />
        </LinearLayout>
    
    </RelativeLayout>
    

    还有一个小注解,我看到你在布局中使用了fill_parent,但这是deprecated,因为API 级别8,所以你应该改用match_parent :)
    希望这会有所帮助,干杯!

    【讨论】:

    • 感谢您的回复!不幸的是,这也不起作用......我不知道我可能做错了什么......因为它看起来很简单......我已将所有缺少的源代码添加到我的问题中;)
    • 我看不出有什么问题...也许你可以把你的源代码上传到 Github 让我看看?
    • 我只是想为你实现一个简单的例子,它在那里工作 O_o。所以我的示例中可能存在一些布局问题。非常感谢您的帮助!我会接受你的回答;)
    • 经过数小时的调试,我终于发现了错误……我的一些布局有id为“content”的视图。我认为当我查找 R.id.content 时,将使用“最近”元素......事实并非如此!
    • 很高兴听到您发现了这个错误!你的意思是你在相同的布局中使用相同的 id 吗?我一直在不同的布局中使用相同的 id,这从来没有给我带来任何麻烦......
    【解决方案2】:

    您应该使用FrameLayout 作为您的片段容器,并将您的片段放入(然后替换)到该 FrameLayout 容器中。

    【讨论】:

    • 我试过这个 - 但没有区别。添加片段时,我的其他布局仍然消失...
    猜你喜欢
    • 2016-09-05
    • 2014-02-04
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多