添加片段到布局会导致空白UI除了新片段

时间:2015-08-19 19:48:57

标签: android android-layout android-fragments

Rookie android dev,继承了一款运行良好的app。他们想要添加简单的底部导航。只是为了看看它的外观,我在片段中添加了一些单选按钮,将该片段包含在主活动页面的布局中,并让主活动类实现了支持新片段所需的接口。主活动类扩展了一个扩展FragmentActivity的自定义类。

在添加我的应用程序后,除了空白屏幕下方的单选按钮外,主活动屏幕上不会显示任何内容。更糟糕的是,如果我触摸空白区域,该应用程序会响应触摸,因此显然它认为它仍然显示应该显示的内容(地图和商家列表,触摸导航到触摸的商家的详细信息页面)

最糟糕的是,没有编译或运行时错误。在这一点上甚至不确定谷歌是什么......欢迎任何帮助。我已经尝试将底部导航的大小一直缩小到几乎看不见,我试图缩小主页上的其他组件。

这是主页面的布局;几乎绝对的底部是我添加新的包括:

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

1 个答案:

答案 0 :(得分:0)

fragment_bottom_nav.xml中 - 将android:layout_alignParentBottom="true"行从RadioGroup移至父RelativeLayout(同样,您可能希望RelativeLayout的高度为wrap_contentmatch_parent而不是fragment_bottom_nav.xml)。当/如果您在Android Studio设计器中查看它时,请务必查看主要布局,而不是protected static final int REQ_CODE_PICK_BANNER = 2; //identificador del inten private static final int BANNER_WIDTH_PX = 640; private static final int BANNER_HEIGHT_PX =244; protected void startSelectorBanner(){ Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); photoPickerIntent.setType("image/*"); photoPickerIntent.putExtra("crop", "true"); photoPickerIntent.putExtra("aspectX",BANNER_WIDTH_PX); photoPickerIntent.putExtra("aspectY",BANNER_HEIGHT_PX); photoPickerIntent.putExtra("outputX",BANNER_WIDTH_PX); photoPickerIntent.putExtra("outputY",BANNER_HEIGHT_PX); photoPickerIntent.putExtra("return-data", true); photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); startActivityForResult(photoPickerIntent, REQ_CODE_PICK_BANNER); } public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { getMainActivity().removeOnActivityResultsListeners(EditQcardFragment.this); switch (requestCode) { case REQ_CODE_PICK_BANNER: if (resultCode == Activity.RESULT_OK) { if (imageReturnedIntent!=null) { Bundle extras = imageReturnedIntent.getExtras(); Bitmap selectedBitmap = extras.getParcelable("data"); int[] screenSize = Screen.getSize(getActivity()); int bannerX = screenSize[0]; int bannerY = (int) (screenSize[0]/BANNER_RATIO); Bitmap resizedBitmap = processPicture(selectedBitmap,"banner.png",bannerX,bannerY); mBytesBanner = getPictureBytes("banner.png", resizedBitmap); log.debug("Get png width: " + mBytesBanner.length); if(mBytesBanner!=null){ mBanner.setImageBitmap(resizedBitmap); } } }break; } } (此处它看起来全部与顶部对齐)