繁体   English   中英

为什么没有出现FloatingActionButton?

[英]Why does not appear FloatingActionButton?

我将问题改为一个更容易理解的问题。 但是现在还有另一个问题。 为什么没有出现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;
    }
}

您的类扩展了MyAppCompat类。 在其中创建FloatingActionButton ,然后使用addContentView方法添加FAB。 您的Home类扩展了MyAppCompat类。 onCreate方法上,您首先调用超级方法,该方法将添加FAB。 但是随后您调用setContentView并设置一个新的布局,该布局将替换先前添加的FAB。 可能的解决方法是先添加布局,然后再添加FAB。 但是您必须首先从AppCompatActivity调用原始的onCreate。 因此,在MyAppCompat类中创建一个名为addFAB的新方法,并将FAB的代码放在此处。 然后在您的家庭课堂上做类似的事情

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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