【问题标题】:Do we need to specific its UI components (Spinner, EditText) colors explicitly when using AppCompat使用 AppCompat 时是否需要明确指定其 UI 组件(Spinner、EditText)颜色
【发布时间】:2015-05-12 03:16:44
【问题描述】:

以前,我有一个 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 时?

注意,我没有在ActivityDialogFragment 中指定主题。我只在Application 中指定了主题,并且我希望我的ActivityDialogFragment 将继承自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);

        // http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked
        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);        
}

【问题讨论】:

  • 你使用了support.v7版本的AlertDialog吗?
  • 那么实际创建对话框的代码在哪里?
  • @ianhanniballake 我更新了这个问题。这是创建和显示对话框片段的一种非常标准的方式。
  • 不,我说的是 onCreateDialog() 中的代码来实际创建对话框 - 您只包含了构建视图的代码,而不是对话框本身。
  • @ianhanniballake 好的。我明白你的意思。已编辑。如果您需要进一步说明,请告诉我。

标签: android android-layout android-appcompat


【解决方案1】:

AlertDialog 的支持库版本 support.v7.app.AlertDialog 添加到 Android Support Library 22.1 中,为所有 API7+ 设备带来了单一的材质样式对话框(以及内部视图的主题化)。如果您使用 AppCompat,您还应该使用支持库 AlertDialog

【讨论】:

    【解决方案2】:

    主题Theme.Sherlock.Light 在技术上是Holo 家族Holo.Light 的兼容版本。在appcompat 库中,主题Theme.AppCompat.Light 是Material family 的兼容版本。所以上面的行为是可以预料的,因为像 Spinner 和 EditText 这样的小部件是 styled 具有新的材料设计。

    如果您想保留 Holo 外观,则必须从 values-v21 开始指定您自己的样式,您稍后会在 v14 和 v11 中继承这些样式。这篇文章展示了如何为 spinner 做这件事:Styling a Spinner, appcompat, material

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 2015-09-15
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 2013-04-07
      相关资源
      最近更新 更多