如何在Theme.material中使用TextInputLayout?

时间:2016-09-12 11:48:55

标签: java android material-design

我试图将TextInputLayout与Theme.material一起使用,代码看起来像....

activity_mail.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"
    xmlns:drawableLeft="http://schemas.android.com/apk/res-auto"
    android:background="#2196F3"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    tools:context="com.realmilk.MainActivity">

    <TextView
        android:id="@+id/login_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="22dp"
        android:gravity="center_horizontal"
        android:text="Account Login"
        android:textColor="#fff"
        android:textSize="26sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/login_title"
        android:layout_marginTop="70dp"
        android:background="#fff"
        android:elevation="4dp"
        android:orientation="vertical"
        android:padding="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="30dp">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/ic_supervisor_account_black_24dp"
                    android:drawableTint="#FF4081"
                    android:singleLine="true"
                    android:hint="User Name"
                    android:inputType="textEmailAddress" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:singleLine="true"
                    android:drawableLeft="@drawable/ic_lock_black_24dp"
                    android:drawableTint="#FF4081"
                    android:hint="Password"
                    android:inputType="numberPassword" />
            </android.support.design.widget.TextInputLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:paddingTop="5dp"
                android:text="Forgot Password?" />


            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="22dp"
                android:background="#d67601"
                android:text="Sign in"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>

    <ImageButton
        android:id="@+id/user_profile_photo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/login_title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:background="@drawable/user_profile_image_background"
        android:elevation="4dp" />

</RelativeLayout>

MainActivity看起来像这样..

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

当我尝试执行此

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.                                                                 
           at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
           at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
           at com.realmilk.MainActivity.onCreate(MainActivity.java:11)
           at android.app.Activity.performCreate(Activity.java:6237)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
           at android.os.Handler.dispatchMessage(Handler.java:102) 
           at android.os.Looper.loop(Looper.java:148) 
           at android.app.ActivityThread.main(ActivityThread.java:5417)

这是我观察到的控制台。

任何人都可以帮我找到错误的地方。

2 个答案:

答案 0 :(得分:0)

更改以下文件

<强> style.xml

<resources>

    <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>

<强> colors.xml

<resources>
<color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#1a237e</color>
    <color name="transparent">@android:color/transparent</color>
    <color name="colorAccent">#FF4081</color>
    <color name="windowBackground">#313131</color>

</resources>
  

您可以根据需要更改颜色。

<强>的AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    //your activities

</application>

希望这会有所帮助...

答案 1 :(得分:0)

TextInputLayout是support库的一部分(正如您所看到的那样,它是设计库的一部分)。支持库中的所有支持组件都与其他支持组件(如主题)互补。在这种情况下,您需要使用Theme.AppCompat。但不用担心,所有compat ui元素都是为了支持旧的Android版本而构建的,但如果您使用app compat主题,它会自动使用Material主题实现在您的应用中生成UI。

总结一下:你必须Theme.AppCompat,但它等于Android OS中的Material Theme&gt; = 5.0

只需使用:

<style name="AppTheme" parent="Theme.AppCompat">

而不是标准材料。