Android片段内容隐身/不显示?

时间:2017-09-07 08:12:46

标签: java android xml android-fragments

我是android开发的新手,我已经设置了片段和底部导航栏(根据youtube教程),片段正确切换(" toast"正确返回)但没有片段内容的片段将完全显示(例如按钮或文本视图)。救命啊!

功能选择:

.logistic-bg .polkrys_shadow:after { /* Additional pseudo-element added */
   content: ""; 
   background: white; 
   position: absolute; 
   left: 0; 
   right: 0; 
   top: 0; 
   bottom: 0; 
}

.logistic-bg .polkrys_shadow { /* Adjustments made on parent element */
   z-index: 9;
}

.logistic-bg div:first-child { /* Adjustments made on nested element */
   position: relative; /* required for z-index to apply */
   z-index: 99;
}

片段XML:

    package com.app.myapplication;

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.design.widget.BottomNavigationView;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.view.MenuItem;
    import android.widget.TextView;

    public class FunctionSelection extends AppCompatActivity {


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_weather:
                    transaction.replace(R.id.content,new WeatherFragment()).commit();
                    return true;
                case R.id.navigation_observing:
                    transaction.replace(R.id.content,new ObservingFragment()).commit();
                    return true;
                case R.id.navigation_sattelites:
                    transaction.replace(R.id.content,new SatteliteFragment()).commit();
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function_selection);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        transaction.replace(R.id.content,new WeatherFragment()).commit();


    }

   }

片段代码:

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.myapplication.WeatherFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment" />



</FrameLayout>

功能选择XML:

 package com.app.myapplication;

    import android.content.Context;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.Toast;
    import android.widget.ToggleButton;



    public class WeatherFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public WeatherFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment WeatherFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static WeatherFragment newInstance(String param1, String param2) {
        WeatherFragment fragment = new WeatherFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_weather, container, false);


    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            Toast.makeText(context, "a", Toast.LENGTH_SHORT).show();
        }
    }

    @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>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
    }

谢谢!

3 个答案:

答案 0 :(得分:0)

更改

android:orientation="horizontal"

android:orientation="vertical"

删除 android:layout_weight="1"

答案 1 :(得分:0)

android:orientation =“horizo​​ntal”

的问题

您可以使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="TextView" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    app:menu="@menu/navigation"
    android:background="?android:attr/windowBackground" />


<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_above="@+id/navigation"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize">

</FrameLayout>


</RelativeLayout>

答案 2 :(得分:0)

这里试试这个

  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.myapplication.FunctionSelection"
    android:orientation="horizontal">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:gravity="center"
    android:textSize="10pt"
    android:text="TextView" />


<FrameLayout
    android:id="@+id/content"
    android:layout_width="120dp"
    android:layout_height="match_parent"
    />
<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_gravity="bottom"
    app:menu="@menu/navigation"
    android:background="?android:attr/windowBackground"
    />
</LinearLayout>

问题似乎出现在XML

您已将方向指定为水平方向 并在最后给出FrameLayout ,其高度和宽度为match_parent 这将完全推出Linearlayout

如果需要,可以为布局宽度放置权重。