为什么不出现FloatingActionButton?

时间:2017-03-25 07:38:32

标签: java android

我把问题改成了一个更容易理解的问题。但现在又出现了另一个问题。为什么不出现FloatingActionButton

为什么不出现FloatingActionButton 为什么不出现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;
    }
}

1 个答案:

答案 0 :(得分:0)

您的课程延伸MyAppCompat课程。您在哪里创建FloatingActionButton,然后使用addContentView方法添加FAB。您的Home类扩展了此MyAppCompat类。在onCreate方法中,您首先调用super,然后添加FAB。但是然后你调用setContentView并设置一个新的布局来替换之前添加的FAB。可能的解决方法是先添加布局,然后再添加FAB。但是你必须首先从AppCompatActivity调用原始的onCreate。因此,在addFAB类中创建一个名为MyAppCompat的新方法,并在此处输入FAB的代码。 然后在您的Home课程中执行类似

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