我如何添加时间选择器与正确的应用程序,并具有足够的风格?

时间:2016-08-20 16:02:35

标签: android timepicker

我想知道问题出在哪里,因为我总是得到这个错误:

  

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

     

无法找到当前主题的主题资源?attr / colorAccent   无法找到'?attr / colorAccent'在当前的主题。

Main_activity:

 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" />

String.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>

Style.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>

1 个答案:

答案 0 :(得分:1)

嗯,android:Theme.Light是主题。

?attr/colorAccent是一个属性。

您的错误表明该主题不包含该属性。

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

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