0
我試圖實現新的材質設計浮動EditText,但我收到錯誤。所以,我按照我編譯庫指令通過的build.gradle文件中使用由谷歌最新的「支持Android設計庫」推出的2015年五月29日按如下:材質設計 - 浮動EditText實現
compile 'com.android.support:design:22.2.+'
雖然一個EditText單獨將隱藏提示文本的第一個字符輸入後,你就可以把它包裝在一個TextInputLayout,導致提示文本成爲EditText上上方的浮動標籤,確保用戶永不失去他們所進入的環境。
所以,這裏是我的佈局xml文件的代碼,代碼有問題的是Android的ID:ID = 「@ + ID/viewTest」 和android:ID =「@ + ID/fNameTest:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainFragmentViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<ScrollView
android:id="@+id/scrollableContents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="80dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="android.support.design.widget.TextInputLayout"
android:id="@+id/viewTest"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" >
<EditText
android:id="@+id/fNameTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="First Name"/>
</view>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/roiInitialInvestmentHelpImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:paddingBottom="2dp"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_currency_usd" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/roiInitialInvestmentHelpImageView"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:labelFor="@+id/roiInitialInvestmentEditText"
android:paddingTop="@dimen/activity_double_vertical_margin"
android:text="Initial Investment"
android:textColor="@color/accent"
android:textSize="20sp" />
<EditText
android:id="@+id/roiInitialInvestmentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits=".,"
android:inputType="numberDecimal"
android:text=""
android:textColor="@color/primary_text_default_material_light" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/roiReturnAmountHelpImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:paddingBottom="2dp"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_currency_usd" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/roiReturnAmountHelpImageView"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:labelFor="@+id/roiReturnAmountEditText"
android:paddingTop="@dimen/activity_double_vertical_margin"
android:text="Return Amount"
android:textColor="@color/accent"
android:textSize="20sp" />
<EditText
android:id="@+id/roiReturnAmountEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits=".,"
android:inputType="numberDecimal"
android:text=""
android:textColor="@color/primary_text_default_material_light" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="32dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Period"
android:textColor="@color/accent"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:width="64dp"
android:text="Years"
android:textColor="@color/secondary_text_material_light"
android:textSize="16sp" />
<SeekBar
android:id="@+id/roiYearSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:max="10" />
<TextView
android:id="@+id/roiYearTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:width="40dp"
android:gravity="center"
android:text="0"
android:textColor="@color/secondary_text_material_light"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:width="64dp"
android:text="Months"
android:textColor="@color/secondary_text_material_light"
android:textSize="16sp" />
<SeekBar
android:id="@+id/roiMonthSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:max="11" />
<TextView
android:id="@+id/roiMonthTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:width="40dp"
android:gravity="center"
android:text="0"
android:textColor="@color/secondary_text_material_light"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
我得到的錯誤有以下幾種:
而下面的logcat消息:
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.os.Handler.dispatchMessage(Handler.java:102)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.os.Looper.loop(Looper.java:135)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.ActivityThread.main(ActivityThread.java:5254)
06-26 00:41:33.018: E/AndroidRuntime(11085): at java.lang.reflect.Method.invoke(Native Method)
06-26 00:41:33.018: E/AndroidRuntime(11085): at java.lang.reflect.Method.invoke(Method.java:372)
06-26 00:41:33.018: E/AndroidRuntime(11085): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-26 00:41:33.018: E/AndroidRuntime(11085): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-26 00:41:33.018: E/AndroidRuntime(11085): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class android.support.design.widget.TextInputLayout
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.createView(LayoutInflater.java:633)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
06-26 00:41:33.018: E/AndroidRuntime(11085): at uk.co.codepix.calculator.ui.fragments.RoiInputCalcFrag.onCreateView(RoiInputCalcFrag.java:42)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.Fragment.performCreateView(Fragment.java:2053)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.BackStackRecord.run(BackStackRecord.java:834)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.Activity.performStart(Activity.java:6005)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
06-26 00:41:33.018: E/AndroidRuntime(11085): ... 10 more
06-26 00:41:33.018: E/AndroidRuntime(11085): Caused by: java.lang.reflect.InvocationTargetException
06-26 00:41:33.018: E/AndroidRuntime(11085): at java.lang.reflect.Constructor.newInstance(Native Method)
06-26 00:41:33.018: E/AndroidRuntime(11085): at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.view.LayoutInflater.createView(LayoutInflater.java:607)
06-26 00:41:33.018: E/AndroidRuntime(11085): ... 24 more
06-26 00:41:33.018: E/AndroidRuntime(11085): Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 18
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.content.res.TypedArray.getColor(TypedArray.java:401)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.support.design.widget.CollapsingTextHelper.setCollapsedTextAppearance(CollapsingTextHelper.java:166)
06-26 00:41:33.018: E/AndroidRuntime(11085): at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:106)
06-26 00:41:33.018: E/AndroidRuntime(11085): ... 27 more
任何想法爲什麼這些錯誤發生?
甚至在使用設計庫中的任何api之前,是否可以使用gradle成功構建? –
從支持設計庫中使用新的TextInputLayout時可能發生[RuntimeException]重複(http://stackoverflow.com/questions/30551229/runtimeexception-while-using-new-textinputlayout-from-support-design-library) – tachyonflux
Hi @Radix ...是的,它編譯正確。 – CBA110