java - float 提交按钮

标签 java android

即使添加了 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 父布局应该是 LinearLayoutRelativeLayout 而不是 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/

相关文章:

java - 无法进入 Hive Shell

java - 使用 Java Web Start (JNLP) 部署 *.war 应用程序

java - 如何使用Hibernate session 批量执行SQL查询

java - 我无法获取 jtextfield 中任何数字值的百分比

java - 将 Subversion 代码 checkin Eclipse 后,如何构建它?

java - 如何在 Android 上显示 GoogleMap v2

android - 再次关于 SoundPool 的内存问题

android - 应用程序在 android 中显示图像很慢

android - 我可以让Firebase使用用户名登录过程吗?

android - ScrollView 中的LinearLayout : Make last child have minHeight and fill empty space if necessary