【问题标题】:Floating Submit Button浮动提交按钮
【发布时间】:2014-04-18 07:03:51
【问题描述】:

即使在添加了片段后,活动布局中的提交按钮也会浮动到手机屏幕的左上角,从而导致它遮挡了片段的某些内容。调用以下代码时是否应该不下推:

currentFragment = MyFragment.newInstance(); getSupportFragmentManager().beginTransaction() .replace(R.id.my_fragment, currentFragment).commit();

活动布局:

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

片段布局(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>

我在这里缺少关于片段和布局的什么?

【问题讨论】:

    标签: java android


    【解决方案1】:

    根据您的要求,您的 Activity 父布局应该是 LinearLayoutRelativeLayout 而不是 FrameLayout。还要为Fragment布局设置一个布局权重,这样它就会占据剩余空间,供Button使用

    像这样改变你的活动布局

    <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,您可以在XML 中将Fragment 容器定义为FrameLayout(以任何方式在代码中创建片段实例)。

     <FrameLayout 
        android:id="@+id/my_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="100"/>
    

    希望这有助于解决您的问题。

    【讨论】:

      猜你喜欢
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 2012-04-05
      • 2014-02-08
      相关资源
      最近更新 更多