即使添加了 fragment , Activity 布局中的提交按钮也会 float 到手机屏幕的左上角,从而导致 fragment 的某些内容变得模糊。调用下面的代码时不应该被下推吗:
currentFragment = MyFragment.newInstance(); getSupportFragmentManager().beginTransaction() .replace(R.id.my_fragment, currentFragment).commit();
Activity 布局:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.mycompany.myapp.MyFragment"
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/my_fragment_layout"/>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit_button_text"
/>
</FrameLayout>
fragment 布局(my_fragment_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/question_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"/>
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio_buttons"></RadioGroup>
</LinearLayout>
这里的 fragment 和布局我缺少什么?
最佳答案
根据您的要求,您的 Activity
父布局应该是 LinearLayout
或 RelativeLayout
而不是 FrameLayout
。还为 Fragment
布局设置一个布局权重,以便它将占据剩余空间,供 Button
像这样更改您的 Activity 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/my_fragment_layout"
android:layout_weight="100"/>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit_button_text"/>
</LinearLayout>
这里还有一个建议。如果您以编程方式使用 Fragment Manager
添加 Fragment
,则只需将 Fragment
容器定义为 XML 中的 FrameLayout
(任何在代码中创建 fragment 实例的方式)。
<FrameLayout
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="100"/>
希望这有助于解决您的问题。
关于java - float 提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23143466/