我想根据用户从Spinner中选择的项填充TextInputLayouts。下面是我的XML(带有“CreatePollAnswer”的ID是我想要动态填充的视图):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/create_poll_linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.troychuinard.fanpolls.Fragment.CreateFragment">
<FrameLayout
android:background="@drawable/image_border"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".525">
<Button
android:id="@+id/add_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click to Add Image" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".475">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/create_poll_question_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="@string/create_poll_question" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/how_many_answers_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/how_many_answers_text"
android:textColor="@color/black"
android:textSize="16sp" />
<Spinner
android:id="@+id/number_of_answers_spinner"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:layout_height="24dp"
android:background="@android:drawable/btn_dropdown" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/create_poll_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/create_poll_answer_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
代码:
public class CreateFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private Button mAddImageButton;
private Spinner mSelectNumberofPollAnswers;
private String mSpinnerPosition;
private EditText mCreatePollQuestion;
private OnFragmentInteractionListener mListener;
private View mRootView;
private TextInputLayout mCreatePollAnswer;
private EditText mCreatePollAnswerEditText;
public CreateFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment CreateFragment.
*/
// TODO: Rename and change types and number of parameters
public static CreateFragment newInstance(String param1, String param2) {
CreateFragment fragment = new CreateFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_create, container, false);
mAddImageButton = (Button) mRootView.findViewById(R.id.add_image_button);
mSelectNumberofPollAnswers = (Spinner) mRootView.findViewById(R.id.number_of_answers_spinner);
mCreatePollQuestion = (EditText) mRootView.findViewById(R.id.create_poll_question_editText);
mCreatePollAnswer = (TextInputLayout) mRootView.findViewById(R.id.create_poll_answer);
mCreatePollAnswerEditText = (EditText) mRootView.findViewById(R.id.create_poll_answer_editText);
// Inflate the layout for this fragment
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
R.array.number_of_poll_answers, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
mSelectNumberofPollAnswers.setAdapter(adapter);
mSelectNumberofPollAnswers.setOnItemSelectedListener(new YourItemSelectedListener());
return mRootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
for (int i = 0; i < Integer.parseInt(selected); i++) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
TextInputLayout newAnswer = new TextInputLayout(getActivity().getApplicationContext());
newAnswer = (TextInputLayout) mRootView.findViewById(R.id.create_poll_answer);
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(newAnswer);
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
以下是我收到的当前错误:
java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36)
at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:139)
at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:132)
at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:128)
at com.troychuinard.fanpolls.Fragment.CreateFragment$YourItemSelectedListener.onItemSelected(CreateFragment.java:153)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:924)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913)
at android.widget.AdapterView.access$300(AdapterView.java:51)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
样式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/primary_darker</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:windowBackground">@color/primary</item>
<item name="colorControlNormal">@color/iron</item>
<item name="colorControlActivated">@color/white</item>
<item name="colorControlHighlight">@color/white</item>
<item name="android:textColorHint">@color/iron</item>
<item name="colorButtonNormal">@color/primary_darker</item>
<item name="android:colorButtonNormal">@color/primary_darker</item>
</style>
>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:textColorPrimary">@color/black</item>
<item name="android:textColorSecondary">@color/white</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/black</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@android:color/white</item>
</style>
<style name="ToolBarStyle" parent="Theme.AppCompat">
<item name="android:titleTextColor">@android:color/white</item>
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="actionMenuTextColor">@android:color/white</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.troychuinard.fanpolls">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".FanPollsApplication"
android:allowBackup="true"
android:icon="@drawable/fan_polls_logo"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"> <!-- ADD THIS LINE -->
>
<activity
android:name=".SignupActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<meta-data
android:name="com.firebase.ui.GoogleClientId"
android:value="@string/google_client_id" />
<activity android:name="com.firebase.ui.auth.twitter.TwitterPromptActivity" />
<meta-data
android:name="com.firebase.ui.TwitterKey"
android:value="@string/twitter_app_key" />
<meta-data
android:name="com.firebase.ui.TwitterSecret"
android:value="@string/twitter_app_secret" />
<activity
android:name=".PollActivity"
android:label="@string/title_activity_poll"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".LoadingActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Discussion_Activity"
android:label="@string/title_activity_discussion_"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".HomeActivity" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".CreateActivity"
android:theme="@style/Theme.AppCompat.Light"
>
</activity>
</application>
答案 0 :(得分:0)
在AndroidManifest.xml文件中,在应用程序标记
中添加以下代码android:theme="@style/Theme.AppCompat.Light"