R将无法在针对Android

时间:2016-09-14 19:39:55

标签: android-layout

刚写了一个styles.xml文件,但是所有对R的引用都没有解析。

干净并重建为第一步,但没有解决问题

values \ styles.xml文件

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>



    <style name="ButtonStyle">
        <item name="android:layout_width">R.dimen.dimension_2</item>
        <item name="android:layout_height">R.dimen.dimension_2</item>
        <item name="android:layout_marginTop">R.dimen.dimension_1</item>
        <item name="android:textColor">R.color.color_black</item>
    </style>


</resources>

values \ dimens.xml文件

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="dimension_1">16dp</dimen>
    <dimen name="dimension_2">48dp</dimen>
    <dimen name="dimension_3">8dp</dimen>
    <dimen name="dimension_4">24dp</dimen>
    <dimen name="text_size_1">16sp</dimen>
</resources>

MainActivity.java文件

/**
 * *Below added my unique package name "com.example.android.justjava3"
 */

package com.example.android.justjava3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This app displays an order form to order coffee, and displays the information.
 * quantity is a global variable
 **/
public class MainActivity extends AppCompatActivity {
    int quantity = 0;

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

    /**
     * This method increments and displays quantity.
     */
    public void increment(View view) {
        if (quantity == 100) {      //Allows out of range values initially
            //show an error as toast
            Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity + 1;

        /*
        if (quantity <= 99) {
        quantity = quantity + 1;
        }
        */
        displayQuantity(quantity);
    }

    /**
     * This method decrements and displays quantity.
     */
    public void decrement(View view) {
        if (quantity == 1) {       //Allows out of range values initially
            //show an error as toast
            Toast.makeText(this, "You cannot have less than 1 coffee", Toast.LENGTH_SHORT).show();
            return;
        }
        quantity = quantity - 1;

       /* if (quantity >=1) {
        quantity = quantity - 1;
        }
        */
        displayQuantity(quantity);
    }


    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number); /**I am receiving a message that this should be a resource value **/
    }


    /**
     * This method is called when the order button is clicked.
     */

    public void submitOrder(View view) {
        EditText nameTextView = (EditText) findViewById(R.id.name_text_view);
        String inputName = nameTextView.getText().toString();

        CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
        boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
        Log.v("MainActivity", "Has whipped cream: " + hasWhippedCream);
        CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
        boolean hasChocolate = chocolateCheckBox.isChecked();
        Log.v("MainActivity", "Has chocolate: " + hasChocolate);
        int price = calculatePrice(hasWhippedCream, hasChocolate);
        String priceMessage = createOrderSummary(price, quantity, hasWhippedCream, hasChocolate, inputName);

        //Create email intent using priceMessage string
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/html");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"javamon@mindspring.com"});
        intent.putExtra(Intent.EXTRA_SUBJECT, "JustJava Coffee Order for " + inputName);
        intent.putExtra(Intent.EXTRA_TEXT, priceMessage);
        startActivity(Intent.createChooser(intent, "Send Email"));
    }

    /**
     * Calculates the total price of the order.
     *
     * @param addWhippedCream is whether or not the user wants whipped cream topping
     * @param addChocolate    is whether or not the user wants chocolate topping
     * @return total price
     */
    private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
        int differentialPrice = 5;

        if (addWhippedCream) {
            differentialPrice = differentialPrice + 1;
        }
        if (addChocolate) {
            differentialPrice = differentialPrice + 2;
        }

        return quantity * differentialPrice;
    }


    /**
     * This method creates the order summary and returns a string.
     *
     * @param price           of the order
     * @param addWhippedCream informs whether the customer wants whipped cream
     * @return text summary
     */
    private String createOrderSummary(int price, int quantity, boolean addWhippedCream, boolean addChocolate, String inputName) {
        String priceMessage = getString(R.string.text_line_11) +  inputName;
        priceMessage += "\n" + getString(R.string.text_line_12) + addWhippedCream;
        priceMessage += "\n" + getString(R.string.text_line_13) + addChocolate;
        priceMessage += "\n" + getString(R.string.text_line_14) +        quantity;
        priceMessage += "\n" + getString(R.string.text_line_15) + price;
        priceMessage += "\n" + getString(R.string.text_line_16);
        return priceMessage;
    }

}

**activity_main.xml file**

<?xml version="1.0" encoding="utf-8"?>

<ScrollView 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="com.example.android.justjava3.MainActivity">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/dimension_1"
    android:orientation="vertical">


    <EditText
        android:id="@+id/name_text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_person_name"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/label_text_view_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimension_1"
        android:text="@string/text_line_8"
        android:textAllCaps="true"
        android:textSize="@dimen/text_size_1" />


    <CheckBox
        android:id="@+id/whipped_cream_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimension_1"
        android:paddingLeft="@dimen/dimension_4"
        android:text="@string/text_line_9"
        android:textSize="@dimen/text_size_1" />

    <CheckBox
        android:id="@+id/chocolate_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimension_1"
        android:paddingLeft="@dimen/dimension_4"
        android:text="@string/text_line_10"
        android:textSize="@dimen/text_size_1" />

    <TextView
        android:id="@+id/label_text_view_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimension_1"
        android:text="@string/text_line_1"
        android:textAllCaps="true"
        android:textSize="@dimen/text_size_1" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <Button
            android:id="@+id/button_1"
            style="@style/ButtonStyle"
            android:layout_width="@dimen/dimension_2"
            android:layout_height="@dimen/dimension_2"
            android:onClick="increment"
            android:text="@string/text_line_2"
            android:textAllCaps="true"
            android:textSize="@dimen/text_size_1" />


        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimension_1"
            android:layout_toRightOf="@id/button_1"
            android:text="@string/text_line_3"
            android:textColor="@color/color_black"
            android:textSize="@dimen/text_size_1" />

        <Button
            android:id="@+id/button_2"
            android:layout_width="@dimen/dimension_2"
            android:layout_height="@dimen/dimension_2"
            android:layout_marginTop="@dimen/dimension_1"
            android:layout_toRightOf="@id/quantity_text_view"
            android:onClick="decrement"
            android:text="@string/text_line_4"
            android:textAllCaps="true"
            android:textColor="@color/color_black"
            android:textSize="@dimen/text_size_1" />

    </LinearLayout>


    <Button
        android:id="@+id/button_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimension_1"
        android:onClick="submitOrder"
        android:text="@string/text_line_7"
        android:textAllCaps="true"
        android:textColor="@color/color_black"
        android:textSize="@dimen/text_size_1" />


</LinearLayout>


</ScrollView>

1 个答案:

答案 0 :(得分:0)

<style name="ButtonStyle">
    <item name="android:layout_width">@dimen/dimension_2</item>
    <item name="android:layout_height">@dimen/dimension_2</item>
    <item name="android:layout_marginTop">@dimen/dimension_1</item>
    <item name="android:textColor">@colors/color_black</item>
</style>

像这样使用