我试图在运行时将一些视图注入线性布局,点击一下按钮。
我的测试环境是API 21和API 16. FOr API 21一切都很好我得到了材料微调器。在API 16上进行测试时,我获得了在XML中定义的所有视图的Material微调器,但是当我使用以下方法动态生成和注入一个sinner时,我获得API级别16微调器。
代码:
public void addView(LinearLayout container, String[] spin_array, String hint, int inputType, int tagger) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.add_company_fragment_two_dynamic_content, null);
final Spinner spin_dynamic = (Spinner) addView.findViewById(R.id.email_spinner1);
EditText edt_dynamic = (EditText) addView.findViewById(R.id.edittext_email1);
ImageView remove_dynamic = (ImageView) addView.findViewById(R.id.btn_remove);
edt_dynamic.setHint(hint);
setUpSpinners(spin_array, spin_dynamic);
edt_dynamic.setTag(container.getTag() + "edt" + tagger);
edt_dynamic.setInputType(inputType);
edt_dynamic.setTypeface(roboto_condenced_light);
spin_dynamic.setTag(container.getTag() + "spin" + tagger);
idsMap.put(spin_dynamic.getTag().toString(), edt_dynamic.getTag().toString());
// get height from dimens
int height = (int) getResources().getDimension(R.dimen.lin_height);
// set this height
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
// we are only concerned about top margin here.
layoutParams.setMargins(0, (int) getResources().getDimension(R.dimen.topMargin), 0, 0);
container.addView(addView, 1, layoutParams);
remove_dynamic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
((LinearLayout) addView.getParent()).removeView(addView);
idsMap.remove(spin_dynamic.getTag().toString());
}
});
}
public void setUpSpinners(String[] spin_array, Spinner spin) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, spin_array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}
XML文件add_company_fragment_two_dynamic_content
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sub_layout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="15dp"
android:background="@drawable/rounded_linear"
android:weightSum="5">
<Spinner
android:id="@+id/email_spinner1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:spinnerMode="dropdown" />
<View
android:id="@+id/view31"
android:layout_width="0.3dp"
android:layout_height="match_parent"
android:background="#D3D3D3" />
<EditText
android:id="@+id/edittext_email1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@null"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:textSize="14sp" />
<ImageView
android:id="@+id/btn_remove"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_weight=".5"
android:contentDescription="@string/close_description"
android:src="@drawable/ic_action_remove" />
</LinearLayout>
屏幕抓取:
以下是此活动的styles.xml
条目:
<style name="CompanyPagerTheme" 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>
<item name="colorControlActivated">@color/colorPrimary</item>
<item name="colorControlNormal">#adadad</item>
<item name="colorControlHighlight">@color/colorPrimaryDark</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:textColorHint">#adadad</item>
</style>
这是我的Gradle
条目:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
}
我在这里想念什么?我还发现涟漪在API 21下无法正常工作