将片段添加到布局会导致空白 UI,新片段除外
Adding fragment to layout results in blank UI except for new fragment
菜鸟 android 开发者,继承了一个运行良好的应用程序。他们想添加简单的底部导航。只是为了看看它会是什么样子,我在一个片段中添加了一些单选按钮,将该片段包含在主 activity 页面的布局中,并让主 activity class 实现所需的接口支持新片段。主要的 activity class 扩展了自定义的 class,后者扩展了 FragmentActivity。
在添加了我的 运行 应用后,activity 主屏幕上除了空白屏幕下方的那些单选按钮外什么也没有显示。更糟糕的是,如果我触摸空白区域,应用程序会响应触摸,所以显然它认为它仍在显示它应该显示的内容(地图和企业列表,触摸导航到触摸的企业的详细信息页面) .
最糟糕的是,没有编译错误和运行时错误。在这一点上什至不确定要 Google 什么...欢迎任何帮助。我试过将底部导航栏的大小一直缩小到几乎看不见,我也试过缩小主页上的其他组件。
这是主页的布局;几乎绝对底部是我添加新包含的地方:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="@dimen/content_view_top_margin"
android:background="@color/white">
<RelativeLayout
android:id="@+id/mapContents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:visibility="invisible" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="41.7150"
map:cameraTargetLng="-77.1625"
map:cameraZoom="10"/>
<LinearLayout
android:id="@+id/mapButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0"
android:orientation="horizontal"
android:padding="@dimen/content_view_padding" >
<ImageButton
android:id="@+id/mapCenterLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_marginRight="10dp"
style="@style/GreenButtonLargeText"
android:src="@drawable/map_home_arrow"/>
<Button
android:id="@+id/mapRedoSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="60"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
style="@style/GreenButtonLargeText"
android:text="Redo Search Here" />
<ImageButton
android:id="@+id/mapCashBackToggle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_marginLeft="10dp"
style="@style/GreenButtonLargeText"
android:src="@drawable/icon_percentage"/>
</LinearLayout>
</RelativeLayout>
<ListView
android:id="@+id/lvBusinesses"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="@android:color/transparent"
android:fadingEdgeLength="0dp"
android:overScrollMode="never"
android:visibility="invisible" />
<include
layout="@layout/no_results_found_layout"/>
<LinearLayout
android:id="@+id/llLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/rounded_corners"
android:padding="20dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgSteam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:background="@drawable/loading_animation" />
<ImageView
android:id="@+id/imgLoader"
android:layout_width="100dp"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/tvLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Loading..." />
</LinearLayout>
</RelativeLayout>
<include android:id="@+id/include_header"
layout="@layout/header" />
<!-- this is the only change Ive made -->
<include android:id="@+id/include_bottom_nav"
layout="@layout/bottom_nav" />
<!-- end change -->
<FrameLayout
android:id="@+id/searchFragmentHolder"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" />
</RelativeLayout>
这是我添加的底部导航部分 (include_bottom_nav.xml):
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/b_nav_fragment"
android:name="com.example.activity.BottomNavFragment"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
引用此片段 (BottomNavFragment.java):
package com.example.activity;
import com.example.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
public class BottomNavFragment extends Fragment
{
private View fragmentView;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment BottomNavFragment.
*/
public static BottomNavFragment newInstance ()
{
BottomNavFragment fragment = new BottomNavFragment ();
Bundle args = new Bundle ();
fragment.setArguments ( args );
return fragment;
}
public BottomNavFragment ()
{
// Required empty public constructor
}
@Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate ( savedInstanceState );
}
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
RadioButton radioButton4;
RadioButton radioButton5;
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate ( R.layout.fragment_bottom_nav, container, false );
radioButton1 = (RadioButton) fragmentView.findViewById ( R.id.btnAll );
radioButton1.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton2 = (RadioButton) fragmentView.findViewById ( R.id.btnPicture );
radioButton2.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton3 = (RadioButton) fragmentView.findViewById ( R.id.btnVideo );
radioButton3.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton4 = (RadioButton) fragmentView.findViewById ( R.id.btnFile );
radioButton4.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton5 = (RadioButton) fragmentView.findViewById(R.id.btnMore);
radioButton5.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
return fragmentView;
}
public void toggleButton( CompoundButton buttonView )
{
if ( null == buttonView )
{
System.out.println ( "null button view -- wtf" );
return;
}
switch ( buttonView.getId () )
{
case R.id.btnPicture:
radioButton2.setButtonDrawable ( R.drawable.navbar_pictureselected );
break;
case R.id.btnMore:
radioButton5.setButtonDrawable ( R.drawable.navbar_moreselected );
break;
case R.id.btnAll:
radioButton1.setButtonDrawable ( R.drawable.navbar_allselected );
break;
case R.id.btnFile:
radioButton4.setButtonDrawable ( R.drawable.navbar_fileselected );
break;
case R.id.btnVideo:
radioButton3.setButtonDrawable ( R.drawable.navbar_videoselected );
break;
}
}
private CompoundButton.OnCheckedChangeListener btnNavBarOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked)
{
Intent i = null;
if (isChecked)
{
if ( mListener != null )
{
switch ( buttonView.getId () )
{
// do nav here later
}
mListener.onFragmentInteraction ( buttonView, i );
}
}
}
};
@Override
public void onAttach ( Activity activity )
{
super.onAttach ( activity );
try
{
mListener = (OnFragmentInteractionListener) activity;
} catch ( ClassCastException e )
{
throw new ClassCastException ( activity.toString ()
+ " must implement OnFragmentInteractionListener" );
}
}
@Override
public void onDetach ()
{
super.onDetach ();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
*/
public interface OnFragmentInteractionListener
{
public void onFragmentInteraction ( CompoundButton buttonView, Intent intent );
}
}
绝对让我头疼。感谢您的指导。
编辑:请求 fragment_bottom_nav.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="@drawable/navbar_background"
>
<RadioButton
android:id="@+id/btnAll"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_allselector"
android:text="All"
/>
<RadioButton
android:id="@+id/btnPicture"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_pictureselector"
android:text="Pictures"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="@+id/btnVideo"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_videoselector"
android:text="Videos"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="@+id/btnFile"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_fileselector"
android:text="Files"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="@+id/btnMore"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_moreselector"
android:text="More"
android:layout_marginLeft="5dp"
/>
</RadioGroup>
</RelativeLayout>
在 fragment_bottom_nav.xml
中-将 android:layout_alignParentBottom="true"
行从 RadioGroup
移动到父级 RelativeLayout
(另外,您可能需要 RelativeLayout
为 wrap_content
而不是 match_parent
)。 When/if 您正在 Android Studio 设计器中检查它,确保查看主布局,而不是 fragment_bottom_nav.xml
(这里它看起来全部对齐到顶部)
菜鸟 android 开发者,继承了一个运行良好的应用程序。他们想添加简单的底部导航。只是为了看看它会是什么样子,我在一个片段中添加了一些单选按钮,将该片段包含在主 activity 页面的布局中,并让主 activity class 实现所需的接口支持新片段。主要的 activity class 扩展了自定义的 class,后者扩展了 FragmentActivity。
在添加了我的 运行 应用后,activity 主屏幕上除了空白屏幕下方的那些单选按钮外什么也没有显示。更糟糕的是,如果我触摸空白区域,应用程序会响应触摸,所以显然它认为它仍在显示它应该显示的内容(地图和企业列表,触摸导航到触摸的企业的详细信息页面) .
最糟糕的是,没有编译错误和运行时错误。在这一点上什至不确定要 Google 什么...欢迎任何帮助。我试过将底部导航栏的大小一直缩小到几乎看不见,我也试过缩小主页上的其他组件。
这是主页的布局;几乎绝对底部是我添加新包含的地方:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="@dimen/content_view_top_margin"
android:background="@color/white">
<RelativeLayout
android:id="@+id/mapContents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:visibility="invisible" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="41.7150"
map:cameraTargetLng="-77.1625"
map:cameraZoom="10"/>
<LinearLayout
android:id="@+id/mapButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0"
android:orientation="horizontal"
android:padding="@dimen/content_view_padding" >
<ImageButton
android:id="@+id/mapCenterLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_marginRight="10dp"
style="@style/GreenButtonLargeText"
android:src="@drawable/map_home_arrow"/>
<Button
android:id="@+id/mapRedoSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="60"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
style="@style/GreenButtonLargeText"
android:text="Redo Search Here" />
<ImageButton
android:id="@+id/mapCashBackToggle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_marginLeft="10dp"
style="@style/GreenButtonLargeText"
android:src="@drawable/icon_percentage"/>
</LinearLayout>
</RelativeLayout>
<ListView
android:id="@+id/lvBusinesses"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="@android:color/transparent"
android:fadingEdgeLength="0dp"
android:overScrollMode="never"
android:visibility="invisible" />
<include
layout="@layout/no_results_found_layout"/>
<LinearLayout
android:id="@+id/llLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/rounded_corners"
android:padding="20dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgSteam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:background="@drawable/loading_animation" />
<ImageView
android:id="@+id/imgLoader"
android:layout_width="100dp"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/tvLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Loading..." />
</LinearLayout>
</RelativeLayout>
<include android:id="@+id/include_header"
layout="@layout/header" />
<!-- this is the only change Ive made -->
<include android:id="@+id/include_bottom_nav"
layout="@layout/bottom_nav" />
<!-- end change -->
<FrameLayout
android:id="@+id/searchFragmentHolder"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" />
</RelativeLayout>
这是我添加的底部导航部分 (include_bottom_nav.xml):
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/b_nav_fragment"
android:name="com.example.activity.BottomNavFragment"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
引用此片段 (BottomNavFragment.java):
package com.example.activity;
import com.example.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
public class BottomNavFragment extends Fragment
{
private View fragmentView;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment BottomNavFragment.
*/
public static BottomNavFragment newInstance ()
{
BottomNavFragment fragment = new BottomNavFragment ();
Bundle args = new Bundle ();
fragment.setArguments ( args );
return fragment;
}
public BottomNavFragment ()
{
// Required empty public constructor
}
@Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate ( savedInstanceState );
}
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
RadioButton radioButton4;
RadioButton radioButton5;
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState )
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate ( R.layout.fragment_bottom_nav, container, false );
radioButton1 = (RadioButton) fragmentView.findViewById ( R.id.btnAll );
radioButton1.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton2 = (RadioButton) fragmentView.findViewById ( R.id.btnPicture );
radioButton2.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton3 = (RadioButton) fragmentView.findViewById ( R.id.btnVideo );
radioButton3.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton4 = (RadioButton) fragmentView.findViewById ( R.id.btnFile );
radioButton4.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
radioButton5 = (RadioButton) fragmentView.findViewById(R.id.btnMore);
radioButton5.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
return fragmentView;
}
public void toggleButton( CompoundButton buttonView )
{
if ( null == buttonView )
{
System.out.println ( "null button view -- wtf" );
return;
}
switch ( buttonView.getId () )
{
case R.id.btnPicture:
radioButton2.setButtonDrawable ( R.drawable.navbar_pictureselected );
break;
case R.id.btnMore:
radioButton5.setButtonDrawable ( R.drawable.navbar_moreselected );
break;
case R.id.btnAll:
radioButton1.setButtonDrawable ( R.drawable.navbar_allselected );
break;
case R.id.btnFile:
radioButton4.setButtonDrawable ( R.drawable.navbar_fileselected );
break;
case R.id.btnVideo:
radioButton3.setButtonDrawable ( R.drawable.navbar_videoselected );
break;
}
}
private CompoundButton.OnCheckedChangeListener btnNavBarOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked)
{
Intent i = null;
if (isChecked)
{
if ( mListener != null )
{
switch ( buttonView.getId () )
{
// do nav here later
}
mListener.onFragmentInteraction ( buttonView, i );
}
}
}
};
@Override
public void onAttach ( Activity activity )
{
super.onAttach ( activity );
try
{
mListener = (OnFragmentInteractionListener) activity;
} catch ( ClassCastException e )
{
throw new ClassCastException ( activity.toString ()
+ " must implement OnFragmentInteractionListener" );
}
}
@Override
public void onDetach ()
{
super.onDetach ();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
*/
public interface OnFragmentInteractionListener
{
public void onFragmentInteraction ( CompoundButton buttonView, Intent intent );
}
}
绝对让我头疼。感谢您的指导。
编辑:请求 fragment_bottom_nav.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="@drawable/navbar_background"
>
<RadioButton
android:id="@+id/btnAll"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_allselector"
android:text="All"
/>
<RadioButton
android:id="@+id/btnPicture"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_pictureselector"
android:text="Pictures"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="@+id/btnVideo"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_videoselector"
android:text="Videos"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="@+id/btnFile"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_fileselector"
android:text="Files"
android:layout_marginLeft="5dp"
/>
<RadioButton
android:id="@+id/btnMore"
style="@style/navbar_button"
android:drawableTop="@drawable/navbar_moreselector"
android:text="More"
android:layout_marginLeft="5dp"
/>
</RadioGroup>
</RelativeLayout>
在 fragment_bottom_nav.xml
中-将 android:layout_alignParentBottom="true"
行从 RadioGroup
移动到父级 RelativeLayout
(另外,您可能需要 RelativeLayout
为 wrap_content
而不是 match_parent
)。 When/if 您正在 Android Studio 设计器中检查它,确保查看主布局,而不是 fragment_bottom_nav.xml
(这里它看起来全部对齐到顶部)