【问题标题】:How can i add Time Picker with the correct App them and with adequate style?如何使用正确的应用程序和适当的样式添加时间选择器?
【发布时间】:2016-08-21 01:43:16
【问题描述】:

我想知道问题出在哪里,因为我一直收到这个错误:

缺少样式。是否为此布局选择了正确的主题? 使用布局上方的主题组合框选择不同的布局,或修复主题样式引用。

找不到当前主题的主题资源 ?attr/colorAccent 在当前主题中找不到“?attr/colorAccent”。

主要活动:

 package dz.timepicker;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {
   private TimePicker timePicker1;
   private TextView time;
   private Calendar calendar;
   private String format = "";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
      time = (TextView) findViewById(R.id.tv1);
      calendar = Calendar.getInstance();

      int hour = calendar.get(Calendar.HOUR_OF_DAY);
      int min = calendar.get(Calendar.MINUTE);
      showTime(hour, min);
   }

   public void setTime(View view) {
      int hour = timePicker1.getCurrentHour();
      int min = timePicker1.getCurrentMinute();
      showTime(hour, min);
   }

   public void showTime(int hour, int min) {
      if (hour == 0) {
         hour += 12;
         format = "AM";
      }
      else if (hour == 12) {
         format = "PM";
      } else if (hour > 12) {
         hour -= 12;
         format = "PM";
      } else {
         format = "AM";
      }
      time.setText(new StringBuilder().append(hour).append(" : ").append(min)
      .append(" ").append(format));
   }


}

main_activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/time_pick"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/set_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="180dp"
    android:onClick="setTime"
    android:text="@string/time_save" />

<TimePicker
    android:id="@+id/timePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/set_button"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="24dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/timePicker1"
    android:layout_alignTop="@+id/set_button"
    android:layout_marginTop="67dp"
    android:text="@string/time_current"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/tv1"
    android:labelFor="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:text="@string/time_selected"
    android:textAppearance="?android:attr/textAppearanceMedium" />

字符串.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TimePicker</string>
   <string name="action_settings">Settings</string>
   <string name="time_picker_example">Time Picker Example</string>
   <string name="time_pick">Pick the time and press save button</string>
   <string name="time_save">Save</string>
   <string name="time_selected"></string>
   <string name="time_current">The Time is:</string>
</resources>

样式.xml:

    <resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

AndroidManifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dz.timepicker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="23"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

    标签: android timepicker


    【解决方案1】:

    嗯,android:Theme.Light 是主题。

    ?attr/colorAccent 是一个属性。

    你的错误说主题不包含该属性。

    欢迎您将其添加到您的AppBaseTheme。随意使用任何颜色

    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="colorAccent">#E91E63</item>
    </style>
    

    【讨论】:

    • 我正在使用 eclipse,当我选择它们时。Light it bug ??
    • Eclipse 只是您编写代码的 IDE。它不会导致问题。您是否将该行添加到AppBaseTheme 内的style.xml 中?
    • 是的,可能我的显卡不支持这个主题我有带有 Intel CORE 2 DUO 的 Mobile Intel(R) 4 Series Express Chipset Family
    • 您的硬件与 Android SDK 编译 XML 属性有什么关系?
    猜你喜欢
    • 2019-10-04
    • 2015-06-04
    • 2020-11-23
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2021-06-22
    相关资源
    最近更新 更多