我得到了一个错误,这是我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BMIActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_bmi_activity"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#FFFFFF" />
<RadioGroup
android:id="@+id/rgUnits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar_bmi_activity"
android:layout_margin="10dp"
android:background="@drawable/drawable_radio_group_tab_background"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbMetricUnits"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="0.50"
android:background="@drawable/drawable_units_tab_selector"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="METRIC UNITS"
android:textColor="@drawable/drawable_units_tab_text_color_selector"
android:textSize="16sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/rbUsUnits"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="0.50"
android:background="@drawable/drawable_units_tab_selector"
android:button="@null"
android:checked="false"
android:gravity="center"
android:text="US UNITS"
android:textColor="@drawable/drawable_units_tab_text_color_selector"
android:textSize="16sp"
android:textStyle="bold" />
</RadioGroup>
<LinearLayout
android:id="@+id/llUnitsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/rgUnits"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="@+id/llMetricUnitsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilMetricUnitWeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etMetricUnitWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="WEIGHT (in kg)"
android:inputType="numberDecimal"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilMetricUnitHeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etMetricUnitHeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HEIGHT (in cm)"
android:inputType="numberDecimal"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/llUsUnitsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilUsUnitWeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etUsUnitWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="WEIGHT (in lbs)"
android:inputType="numberDecimal"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:id="@+id/llUsUnitsHeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilUsUnitHeightFeet"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etUsUnitHeightFeet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Feet"
android:inputType="number"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilUsUnitHeightInch"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etUsUnitHeightInch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Inch"
android:inputType="number"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/llDiplayBMIResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="visible"
tools:visibility="visible">
<TextView
android:id="@+id/tvYourBMI"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="YOUR BMI"
android:textAllCaps="true"
android:visibility="invisible"
android:textSize="16sp" />
<TextView
android:id="@+id/tvBMIValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold"
tools:text="15.00" />
<TextView
android:id="@+id/tvBMIType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
tools:text="Normal" />
<TextView
android:id="@+id/tvBMIDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
tools:text="Normal" />
</LinearLayout>
<Button
android:id="@+id/btnCalculateUnits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="35dp"
android:background="@color/colorAccent"
android:text="CALCULATE"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
这里是styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
我尝试将AppCompat更改为MaterialComponents,但它不起作用。但是当我指向LinearLayout标记(com.google.android.Material.TextField.TextInputLayout的父标记)时,它会用红线显示错误,当我移除LinearLayout标记时,它会消失。当我使用太多的LinearLayout堆栈时有什么问题吗?
您是否在应用程序级build.gradle文件中添加了物料依赖项?
如果不是,你需要在你的分级文件中添加这个。
api 'com.google.android.material:material:<VERSION>'