Android Studio application resource reference

Android Studio application resources

When we often write a lot of pages, we will find that there are a lot of repeated words acridine, color acridine, style acridine, which have been repeatedly used. If we re-set every time we write a page or every place we write, this is very troublesome, and It is also not conducive to later maintenance. Because if you find that the same place is wrong, then every place you write the same must be changed again, and if you put these same styles of acridine in a resource, you only need to call the resource each time you use it, so you can also modify it. Just change the resource.

The red circle circled in the screenshot below is where the resources are placed.

Insert picture description here

Set commonly used strings (words)

strings.xml: The file is a file that stores string types.

Chinese characters are also string types, so first write a string tag as shown in the figure below, give the tag a name, which is the name called for the layout page, and then write the string (character) you want to set in the tag. .

Then the string in the first line is the name of the APP application. Can be modified freely

Insert picture description here

Then it is called on the layout page.

The syntax is "@string/名(name)", where the name is the name you set in the strings.xml file.

Layout page xml file code:

Pay attention to the way the text and hint attributes refer to resources

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="君不见黄河之水天上来,奔流到海不复回"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.116" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="28dp"
        android:text="@string/usename"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="84dp"
        android:text="@string/usepass"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.152"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:hint="@string/namehint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.415"
        app:layout_constraintStart_toEndOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit2"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:hint="@string/passhint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.402"
        app:layout_constraintStart_toEndOf="@+id/textView5"
        app:layout_constraintTop_toBottomOf="@+id/edit" />


</androidx.constraintlayout.widget.ConstraintLayout>

Effect picture:

Insert picture description here

Set the common size value (sp or dp)

The font size sp or the value of the size can also be called by placing the resource file and then laying out the page.

The value of the size such as sp and dp can also be placed in the strings.xml file and then called, but this will be messy and not conducive to maintenance. So create another resource file to store these dp values. New method:

1. Right click on values ​​-> New -> XML -> Values ​​XML File

Insert picture description here

2. Give this file a name, depending on the name.

After it is built, you can start to set the value of sp or dp.

The label corresponding to the size value is, and then add the name, and the corresponding size value is written in the two labels, as shown in the figure below:

Insert picture description here

Layout page XML file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="君不见黄河之水天上来,奔流到海不复回"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.116" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="@dimen/textwsize"
        android:layout_height="wrap_content"
        android:textSize="@dimen/texts"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="28dp"
        android:text="@string/usename"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="@dimen/textwsize"
        android:layout_height="wrap_content"
        android:layout_marginTop="76dp"
        android:text="@string/usepass"
        android:textSize="@dimen/texts"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.152"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="@dimen/ediwsize"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:hint="@string/namehint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.415"
        app:layout_constraintStart_toEndOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit2"
        android:layout_width="@dimen/ediwsize"
        android:layout_height="wrap_content"
        android:hint="@string/passhint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.402"
        app:layout_constraintStart_toEndOf="@+id/textView5"
        app:layout_constraintTop_toBottomOf="@+id/edit" />


</androidx.constraintlayout.widget.ConstraintLayout>

Effect picture:

Insert picture description here

Set color resources

The color resources are stored in colors.xml,

Just set it in it, it's similar to other setting methods, but the label name is different.

colors.xml code:

The above three are from the beginning, and the latter two are written by myself.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>

    <color name="colortext">#010303</color>
    <color name="colortop">#4d4</color>
</resources>

Layout file XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colortop"
        android:text="君不见黄河之水天上来,奔流到海不复回"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.116" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="@dimen/textwsize"
        android:layout_height="wrap_content"
        android:textSize="@dimen/texts"
        android:textColor="@color/colortext"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="28dp"
        android:text="@string/usename"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="@dimen/textwsize"
        android:layout_height="wrap_content"
        android:layout_marginTop="76dp"
        android:text="@string/usepass"
        android:textSize="@dimen/texts"
        android:textColor="@color/colortext"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.152"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="@dimen/ediwsize"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:hint="@string/namehint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.415"
        app:layout_constraintStart_toEndOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit2"
        android:layout_width="@dimen/ediwsize"
        android:layout_height="wrap_content"
        android:hint="@string/passhint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.402"
        app:layout_constraintStart_toEndOf="@+id/textView5"
        app:layout_constraintTop_toBottomOf="@+id/edit" />


</androidx.constraintlayout.widget.ConstraintLayout>

Effect picture:

Insert picture description here

Style file operation

The format is as follows:

<resources>

    <style name="样式文件名" parent="父样式表">
          <item name="定义的属性">属性值</item>
    </style>

</resources>

To use an existing style, add attributes under the layout file XML as follows:

style="@style/样式文件名"

Specific attribute values ​​can be set directly in the style file, or attribute values ​​of other resources can be referenced.

Style code:

<resources>

    <style name="mystyle">
        <item name="android:text">君不见黄河之水天上来,奔流到海不复回</item>
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:textSize">@dimen/toptext</item>
    </style>

</resources>

Layout file XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        style="@style/mystyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.116" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="@dimen/textwsize"
        android:layout_height="wrap_content"
        android:textSize="@dimen/texts"
        android:textColor="@color/colortext"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="28dp"
        android:text="@string/usename"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="@dimen/textwsize"
        android:layout_height="wrap_content"
        android:layout_marginTop="76dp"
        android:text="@string/usepass"
        android:textSize="@dimen/texts"
        android:textColor="@color/colortext"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.152"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="@dimen/ediwsize"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:hint="@string/namehint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.415"
        app:layout_constraintStart_toEndOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/edit2"
        android:layout_width="@dimen/ediwsize"
        android:layout_height="wrap_content"
        android:hint="@string/passhint"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.402"
        app:layout_constraintStart_toEndOf="@+id/textView5"
        app:layout_constraintTop_toBottomOf="@+id/edit" />


</androidx.constraintlayout.widget.ConstraintLayout>

effect:

Insert picture description here

Picture resource reference

You can directly drag the picture into the "drawable" or "mipmap" directory under the "res" directory.

As follows: then click OK

Insert picture description here

Then refer to the layout file XML:

"@drawable/图片名"

I added a background image to the entire page and wrote the added code. Others are the same as above.

android:background="@drawable/img2"

Then look at the renderings:

The picture is a little distorted
Insert picture description here

Guess you like

Origin blog.csdn.net/leilei__66/article/details/109113531