EditText和TextInputLayout以编程方式添加不采摘风格,不显示软键盘

我正在开发一个库模块,我希望允许客户端为EditText和TextInputLayout UI组件传递样式资源。 为了使用传递的布局,我需要在运行时对这两个组件进行充气,并将它们添加到父布局中。 当我这样做,一个奇怪的行为发生:

  1. InputTextLayout不会选择传递的样式;
  2. 点击EditText时,软键盘不会被触发。

父级布局

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar_wrapper" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/search_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways"> <LinearLayout android:id="@+id/toolbar_inner_container" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:gravity="center_vertical" android:orientation="horizontal" android:padding="3dip" android:weightSum="7"> <ImageView android:id="@+id/back" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/ic_arrow_left_white_24dp" /> </LinearLayout> </android.support.v7.widget.Toolbar> <br.com.edsilfer.kotlin_support.presenter.layout.LineProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" app:colorLine="@color/clr_theme_light_text" /> 

样本预设样式:

  <!-- THEME LIGHT --> <!-- search bar input text --> <style name="TextInputThemeDark"> <item name="android:colorAccent">@color/clr_theme_dark_text_input_hint</item> <item name="android:textColorHint">@color/clr_theme_dark_text_input_hint</item> <item name="android:textColor">@color/clr_theme_dark_text_input</item> <item name="android:colorControlNormal">@color/clr_theme_dark_text_input</item> <item name="android:colorControlActivated">@color/clr_theme_dark_text_input</item> <item name="android:textSize">@dimen/dim_presets_commons_text_input_size</item> </style> <style name="SearchText"> <item name="android:textColorHint">#000</item> <item name="android:colorAccent">#000</item> <item name="android:textColor">#000</item> </style> 

以编程方式添加视图

 TextInputLayout iw = new TextInputLayout(mActivity, null, R.style.SearchText); iw.layoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 5f); mInput = EditText(mActivity, null, R.style.TextInputThemeDark); mInput.layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); mClear = new ImageView(mActivity); mClear.layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f); mClear.setImageResource(R.drawable.ic_close_black_24dp); iw.addView(mInput); iw.setHint("Type something..."); ((LinearLayout) mActivity.findViewById(R.id.toolbar_inner_container)).addView(iw) ((LinearLayout) mActivity.findViewById(R.id.toolbar_inner_container)).addView(mClear) 

完整的项目可以在这里找到: https : //github.com/edsilfer/android-search-interface

问题是我实例化UI组件的方式。 相反,以正确的方式调用构造函数,我需要通过使用ContextThemeWrapper 。 我在Kotlin的最终代码是这样出来的:

  val container = LinearLayout(mActivity) val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT) container.layoutParams = params container.weightSum = 7f container.gravity = Gravity.CENTER_VERTICAL container.orientation = LinearLayout.HORIZONTAL mBack.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f) mBack.image = mActivity.getDrawable(mPreset.iconBack) mBack.background = mActivity.getDrawable(R.drawable.rsc_rounded_corners) val iw = TextInputLayout(ContextThemeWrapper(mActivity, mPreset.textInputLayoutStyle)) iw.layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 5f) mInput.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 5f) mInput.background = null iw.addView(mInput) iw.hint = mActivity.getString(mPreset.hintText) mClear.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f) mClear.image = mActivity.getDrawable(mPreset.iconClear) mClear.background = mActivity.getDrawable(R.drawable.rsc_rounded_corners) val toolbar = mActivity.findViewById(R.id.search_toolbar) as Toolbar toolbar.setBackgroundColor(mActivity.resources.getColor(mPreset.colorPrimary)) container.addView(mBack) container.addView(iw) container.addView(mClear) (mActivity.findViewById(R.id.search_toolbar) as Toolbar).addView(container) mInput.requestFocus()