简体   繁体   中英

Why does not appear FloatingActionButton?

I changed my question to a more understandable one. But now there was another problem. Why does not appear FloatingActionButton

Why does not appear FloatingActionButton Why does not appear FloatingActionButton


public class Home extends MyAppCompact 
{
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home); //This is problem
    }

}

//

public class MyAppCompact extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout.LayoutParams layoutParams=new LinearLayout.
                LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        FloatingActionButton floatingActionButton=new FloatingActionButton(this);
        layoutParams.gravity= Gravity.BOTTOM|Gravity.RIGHT;
        layoutParams.setMargins(16,16,16,16);

        floatingActionButton.setLayoutParams(layoutParams);
        floatingActionButton.setImageResource(R.drawable.ic_mic_white_24dp);
        floatingActionButton.setClickable(true);

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                Bitmap bitmap=getScreenShot();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                Intent intent = new Intent(MyAppCompact.this, FeedBack.class);
                intent.putExtra("bitmapData", byteArray);
                startActivity(intent);
            }
        });

        LinearLayout.LayoutParams layoutParams1=new LinearLayout.
                LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        );

        addContentView(floatingActionButton,layoutParams1);

        //
//            <android.support.design.widget.FloatingActionButton
//        android:layout_width="wrap_content"
//        android:layout_height="wrap_content"
//        android:layout_gravity="bottom|right"
//        android:layout_margin="16dp"
//        android:id="@+id/btnFloating"
//        android:clickable="true"
//        android:src="@drawable/ic_mic_white_24dp" />
    }

    public Bitmap getScreenShot() {
        View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
        View screenView = rootView.getRootView();
        screenView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
        screenView.setDrawingCacheEnabled(false);
        return bitmap;
    }
}

Your class extends MyAppCompat class. Where you create the FloatingActionButton and then use addContentView method to add the FAB. Your Home class extends this MyAppCompat class. On the onCreate method you call the super first which adds the FAB. But then you call setContentView and set a new layout which replaces the FAB previously added. Possible fix is to add the layout first and then the FAB. But you have to call the original onCreate from AppCompatActivity first. So create a new method called addFAB in your MyAppCompat class and put the code for the FAB here. Then in your Home class do something like

 public class Home extends MyAppCompact 
{
@Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home); 
        addFAB();
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM