使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Do we need to specific its UI components (Spinner, EditText) colors explicitly when using AppCompat
以前,我有一个 DialogFragment
,我没有明确指定它的 UI 组件(Spinner、EditText)颜色。
dialog_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical" >
<Spinner
android:id="@+id/country_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:layout_marginBottom="10dp" />
<EditText
android:id="@+id/name_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textCapWords"
android:maxLength="50"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
我正在使用
- SherlockActionBar
- Theme.Sherlock.Light.DarkActionBar
- android:minSdkVersion="10", android:targetSdkVersion="21"
看起来如下
迁移到
之后
- AppCompat
- Theme.AppCompat.Light.DarkActionBar
- android:minSdkVersion="10", android:targetSdkVersion="22"
看起来像下面这样
我想知道,我在迁移过程中是否做错了什么? DialogFragment
UI 组件(Spinner、EditText)颜色的特定颜色真的有必要吗?
有没有办法让 DialogFragment
看起来不错,而无需像我们所做的那样手动为 Spinner、EditText 等分配颜色(我们让系统决定最佳颜色Spinner, EditText, ...) 当我们使用 SherlockActionBar 时?
注意,我没有在 Activity
或 DialogFragment
中指定主题。我只有 Application
中的特定主题,我希望我的 Activity
或 DialogFragment
会继承 Application
的。
<application
android:theme="@style/..." >
DialogFragment
的代码是
DialogFragment 的源代码
public class MyDialogFragment extends android.support.v4.app.DialogFragment {
public static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_fragment_layout, null);
final Spinner countrySpinner = (Spinner)view.findViewById(R.id.country_spinner);
final EditText nameEditText = (EditText)view.findViewById(R.id.name_edit_text);
final CountryArrayAdapter countryArrayAdapter = new CountryArrayAdapter(this.getActivity());
countrySpinner.setAdapter(countryArrayAdapter);
nameEditText.setHint(R.string.new_watchlist_hint);
nameEditText.setText(org.yccheok.jstock.watchlist.Utils.getDefaultWatchlistName());
Utils.placeCursorAtEndOfText(nameEditText);
final AlertDialog dialog = new AlertDialog.Builder(this.getActivity())
.setTitle(R.string.new_watchlist_title)
.setView(view)
// Add action buttons
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.create();
dialog.setCanceledOnTouchOutside(true);
//
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
...
}
});
}
});
return dialog;
}
显示 DialogFragment 的代码
private void showMyDialogFragment() {
FragmentManager fm = this.getActivity().getSupportFragmentManager();
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.setTargetFragment(this, 0);
myDialogFragment.show(fm, MY_DIALOG_FRAGMENT);
}
AlertDialog 的支持库版本 support.v7.app.AlertDialog, added in Android Support Library 22.1 为所有 API7+ 设备带来了一个 material 样式的对话框(以及其中的视图主题)。如果您使用的是 AppCompat,则还应该使用支持库 AlertDialog
.
主题 Theme.Sherlock.Light
在技术上是 Holo 家族 Holo.Light 的兼容版本。在 appcompat 库中,主题 Theme.AppCompat.Light
是 Material 系列的兼容版本。所以上面的行为是可以预料的,因为 Spinner 和 EditText 等小部件 styled 具有新的 material 设计。
如果您想保留 Holo 外观,则必须从 values-v21
开始指定您自己的样式,您稍后会在 v14 和 v11 中继承这些样式。此 post 展示了如何为微调器执行此操作:
以前,我有一个 DialogFragment
,我没有明确指定它的 UI 组件(Spinner、EditText)颜色。
dialog_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical" >
<Spinner
android:id="@+id/country_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:layout_marginBottom="10dp" />
<EditText
android:id="@+id/name_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textCapWords"
android:maxLength="50"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
我正在使用
- SherlockActionBar
- Theme.Sherlock.Light.DarkActionBar
- android:minSdkVersion="10", android:targetSdkVersion="21"
看起来如下
迁移到
之后- AppCompat
- Theme.AppCompat.Light.DarkActionBar
- android:minSdkVersion="10", android:targetSdkVersion="22"
看起来像下面这样
我想知道,我在迁移过程中是否做错了什么? DialogFragment
UI 组件(Spinner、EditText)颜色的特定颜色真的有必要吗?
有没有办法让 DialogFragment
看起来不错,而无需像我们所做的那样手动为 Spinner、EditText 等分配颜色(我们让系统决定最佳颜色Spinner, EditText, ...) 当我们使用 SherlockActionBar 时?
注意,我没有在 Activity
或 DialogFragment
中指定主题。我只有 Application
中的特定主题,我希望我的 Activity
或 DialogFragment
会继承 Application
的。
<application
android:theme="@style/..." >
DialogFragment
的代码是
DialogFragment 的源代码
public class MyDialogFragment extends android.support.v4.app.DialogFragment {
public static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_fragment_layout, null);
final Spinner countrySpinner = (Spinner)view.findViewById(R.id.country_spinner);
final EditText nameEditText = (EditText)view.findViewById(R.id.name_edit_text);
final CountryArrayAdapter countryArrayAdapter = new CountryArrayAdapter(this.getActivity());
countrySpinner.setAdapter(countryArrayAdapter);
nameEditText.setHint(R.string.new_watchlist_hint);
nameEditText.setText(org.yccheok.jstock.watchlist.Utils.getDefaultWatchlistName());
Utils.placeCursorAtEndOfText(nameEditText);
final AlertDialog dialog = new AlertDialog.Builder(this.getActivity())
.setTitle(R.string.new_watchlist_title)
.setView(view)
// Add action buttons
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.create();
dialog.setCanceledOnTouchOutside(true);
//
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
...
}
});
}
});
return dialog;
}
显示 DialogFragment 的代码
private void showMyDialogFragment() {
FragmentManager fm = this.getActivity().getSupportFragmentManager();
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.setTargetFragment(this, 0);
myDialogFragment.show(fm, MY_DIALOG_FRAGMENT);
}
AlertDialog 的支持库版本 support.v7.app.AlertDialog, added in Android Support Library 22.1 为所有 API7+ 设备带来了一个 material 样式的对话框(以及其中的视图主题)。如果您使用的是 AppCompat,则还应该使用支持库 AlertDialog
.
主题 Theme.Sherlock.Light
在技术上是 Holo 家族 Holo.Light 的兼容版本。在 appcompat 库中,主题 Theme.AppCompat.Light
是 Material 系列的兼容版本。所以上面的行为是可以预料的,因为 Spinner 和 EditText 等小部件 styled 具有新的 material 设计。
如果您想保留 Holo 外观,则必须从 values-v21
开始指定您自己的样式,您稍后会在 v14 和 v11 中继承这些样式。此 post 展示了如何为微调器执行此操作: