没有XML的片段和CustomView

时间:2017-05-30 03:35:57

标签: android android-fragments android-custom-view

我试图在不使用XML文件的情况下使用Fragments。 我的CustomView正在创建,但它的大小为零,因此没有调用onDraw。

我已经尝试过不同的方法,例如这里提到的方法:Custom onDraw() method not called 但我仍然不认为是画画。

这是我的CustomView类:

public class CameraView extends ConstraintLayout {

    View view;

    public CameraView(Context context) {
        super(context);
        initViews();
        initLayouts();
    }

    void initViews() {
        view = new View(this.getContext());
        view.setBackgroundColor(Color.RED);
        addView(view);
    }

    void initLayouts() {
        // --------------------------------------------------
        // use Android layout params to position subviews
        // within this custom view class
        // --------------------------------------------------

        view.setId(View.generateViewId());
        view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_CONSTRAINT, LayoutParams.MATCH_CONSTRAINT));

        ConstraintSet set = new ConstraintSet();
        set.connect(view.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
        set.connect(view.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
        set.connect(view.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
        set.connect(view.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
        set.applyTo(this);
    }
}

这是片段类:

public class CameraFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        CameraView customView = new CameraView(getContext());
        customView(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return customView;
    }
}

我注意到onCreateView ViewGroup container参数,根据文档,可用于生成视图的LayoutParams。

  

ViewGroup容器:如果为非null,则这是片段的UI应附加到的父视图。该片段不应该添加视图本身,但是这可以用于生成视图的LayoutParams。

但我不确定我是怎么做的,为了解决我的问题。

另外,我也尝试了另一种方法。而是在方法customView上创建我的onCreateView,我创建了一个容器并添加了customView后者,如此post中所做的那样:

public class CameraFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        return linearLayout;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        CameraView cameraView = new CameraView(getContext());
        cameraView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        LinearLayout linearLayout = (LinearLayout) view;
        linearLayout.addView(cameraView);
    }

但它也没有用。

编辑:

  

adb shell dumpsys activity top的输出?

TASK com.qwantech.blupper id=914
  ACTIVITY com.qwantech.blupper/.MainActivity 7fbc9c7 pid=18837
    Local FragmentActivity 83a63ad State:
      mCreated=truemResumed=true mStopped=false mReallyStopped=false
      mLoadersStarted=true
    Active Fragments in 27f7663:
      #0: CameraFragment{9e9c260 #0 id=0x1}
        mFragmentId=#1 mContainerId=#1 mTag=null
        mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
        mAdded=true mRemoving=false mFromLayout=false mInLayout=false
        mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
        mRetainInstance=false mRetaining=false mUserVisibleHint=true
        mFragmentManager=FragmentManager{27f7663 in HostCallbacks{8013319}}
        mHost=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
        mContainer=android.support.constraint.ConstraintLayout{46a8fde V.E...... ........ 0,0-1536,2000 #1}
        mView=android.widget.LinearLayout{ce915bf V.E...... ......I. 0,0-0,0}
        mInnerView=android.widget.LinearLayout{ce915bf V.E...... ......I. 0,0-0,0}
        Child FragmentManager{498a48c in CameraFragment{9e9c260}}:
          FragmentManager misc state:
            mHost=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
            mContainer=android.support.v4.app.Fragment$2@768e7d5
            mParent=CameraFragment{9e9c260 #0 id=0x1}
            mCurState=5 mStateSaved=false mDestroyed=false
    Added Fragments:
      #0: CameraFragment{9e9c260 #0 id=0x1}
    FragmentManager misc state:
      mHost=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
      mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@8013319
      mCurState=5 mStateSaved=false mDestroyed=false
    View Hierarchy:
      com.android.internal.policy.PhoneWindow$DecorView{278f248 V.E..... ... 0,0-1536,2048}
        android.widget.LinearLayout{20bcbea V.E..... ... 0,0-1536,2048}
          android.view.ViewStub{8234edb G.E..... ... 0,0-0,0 #102046a android:id/action_mode_bar_stub}
          android.widget.FrameLayout{1717d78 V.E..... ... 0,48-1536,2048}
            android.support.v7.widget.FitWindowsLinearLayout{45d2c51 V.E..... ... 0,0-1536,2000 #7f0b004a app:id/action_bar_root}
              android.support.v7.widget.ViewStubCompat{6d2bcb6 G.E..... ... 0,0-0,0 #7f0b004b app:id/action_mode_bar_stub}
              android.support.v7.widget.ContentFrameLayout{7fdfdb7 V.E..... ... 0,0-1536,2000 #1020002 android:id/content}
                android.support.constraint.ConstraintLayout{46a8fde V.E..... ... 0,0-1536,2000 #1}
                  android.widget.LinearLayout{ce915bf V.E..... ... 0,0-0,0}
                    com.qwantech.blupper.CameraView{cfcf924 V.E..... ... 0,0-0,0}
                      android.view.View{1773c8d V.ED.... ... 0,0-0,0 #2}
        android.view.View{645ee42 V.ED.... ... 0,0-1536,48 #102002f android:id/statusBarBackground}

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        ConstraintLayout container = new ConstraintLayout(getApplicationContext());
        container.setId(View.generateViewId());
        container.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        container.setBackgroundColor(Color.CYAN);
        setContentView(container);

        CameraFragment f = CameraFragment.newInstance();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(container.getId(), f);
        fragmentTransaction.commit();

    }
}

0 个答案:

没有答案