将类android.support.design.widget.CoordinatorLayout膨胀以查看错误

时间:2017-09-26 06:21:07

标签: android layout-inflater android-support-design

我想夸大下面的布局,但它给了我一个错误。

<android.support.design.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="false">
      ....
</android.support.design.widget.CoordinatorLayout>

当我尝试这个给我错误的代码时

 LayoutInflater.from(getApplicationContext()).inflate(R.layout.fooview, null, false)

我将设置

1)AppTheme.NoActionBar样式activity

2)添加了'com.android.support:design:26.+'个依赖项

3)并Activity延伸AppCompatActivity

日志中的

错误

  

引起:android.view.InflateException:二进制XML文件行#14:二进制XML文件行#14:错误膨胀类android.support.design.widget.CoordinatorLayout

2 个答案:

答案 0 :(得分:0)

为您创建样本。它的工作对我来说希望它对你有所帮助

public class MyFragment extends Fragment {

    private View rootView;
    private TextView sampleText;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_sample_two,null,false);
        sampleText = rootView.findViewById(R.id.txtView);
        sampleText.setText("Hello Code Sample");
        return rootView;

    }
}

Gradle依赖项:

compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.android.support:appcompat-v7:26.+'

Xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:text="Code Sample"
        android:layout_height="wrap_content" />

</android.support.design.widget.CoordinatorLayout>

对我来说没有错误。您必须添加依赖项。

答案 1 :(得分:0)

使用活动:

public class HomeActivity extends AppCompatActivity {

    private LinearLayout linearLy;
    private CoordinatorLayout coordinatorLayout;
    private TextView txtTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        linearLy = (LinearLayout) findViewById(R.id.linearLy);
        coordinatorLayout = (CoordinatorLayout) getLayoutInflater().inflate(R.layout.activity_sample_two, null, false);
        txtTextView = (TextView) coordinatorLayout.findViewById(R.id.txtView);
        txtTextView.setText("Hello Sample");
        linearLy.addView(coordinatorLayout);
    }

}

MainLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLy"
    android:orientation="vertical"
    android:background="@android:color/holo_blue_bright"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

膨胀的activity_sample_two.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@android:color/darker_gray"
    android:fitsSystemWindows="false">

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:text="Code Sample"
        android:layout_height="wrap_content" />

</android.support.design.widget.CoordinatorLayout>

如果需要更多帮助,请告诉我们!