android开发基础7-android应用的资源

前言

资源:布局、颜色、字符串、图片等等

android把常用的资源同一放在res目录下

字符串资源

1、定义字符串资源文件
2、使用字符串资源:在xml文件中使用,在java文件中使用

默认字符串资源文件:res/values/strings.xml

<resources>
    <string name="app_name">jiyujianting</string>
    <string name="showme">showme</string>
</resources>

在xml中使用字符串资源

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button_1"
    android:text="@string/showme" />

在java中使用字符串资源 - 一般动态设置的时候

 Button button = findViewById(R.id.button_1);
 button.setText(R.string.showme);
 button.setText(getResources().getString(R.string.showme));

一个实例

<resources>
    <string name="app_name">Windows Phone 10.1</string>
    <string name="textView1">微信</string>
    <string name="textView2">通讯录</string>
    <string name="textView3">QQ</string>
    <string name="textView4">相机</string>
    <string name="textView5">时钟</string>
    <string name="textView6">备忘录</string>
    <string name="textView7">音乐</string>
    <string name="textView8">互联网</string>
    <string name="textView9">邮件</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:paddingBottom="80dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="80dp"
    tools:context="com.mingrisoft.MainActivity">
    <!-- 第一行的线性布局管理器 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/textView1"
            android:gravity="center"
            android:text="@string/textView1"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"
            android:layout_weight="1"
            android:background="@color/textView2"
            android:gravity="center"
            android:text="@string/textView2"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"
            android:layout_weight="1"
            android:background="@color/textView3"
            android:gravity="center"
            android:text="@string/textView3"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />

    </LinearLayout>
    <!-- 第二行的线性布局管理器 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/margin"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/textView4"
            android:gravity="center"
            android:text="@string/textView4"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"
            android:layout_weight="1"
            android:background="@color/textView5"
            android:gravity="center"
            android:text="@string/textView5"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"
            android:layout_weight="1"
            android:background="@color/textView6"
            android:gravity="center"
            android:text="@string/textView6"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />
    </LinearLayout>
    <!-- 第三行的线性布局管理器 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/margin"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/textView7"
            android:gravity="center"
            android:text="@string/textView7"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"
            android:layout_weight="1"
            android:background="@color/textView8"
            android:gravity="center"
            android:text="@string/textView8"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />

        <TextView
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin"
            android:layout_weight="1"
            android:background="@color/textView9"
            android:gravity="center"
            android:text="@string/textView9"
            android:textColor="@color/wordcolor"
            android:textSize="@dimen/wordsize"
            />
    </LinearLayout>
</LinearLayout>

颜色资源

通过透明度 + 红绿蓝值

# 透明度 R G B
透明度 00 - FF  : FF表示完全不透明,透明度也可以省略

默认在res/values/colors.xml中定义颜色资源
定义颜色资源

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="wordcolor">#FFFFFF</color>
</resources>

在xml中使用颜色资源

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:gravity="center"
       android:textColor="@color/wordcolor"
       android:textSize="@dimen/wordsize"
       />

在java中使用颜色资源

TextView tv=(TextView)findViewById(tvid[i]);
tv.setBackgroundColor(getResources().getColor(R.color.wordcolor));	

尺寸资源

android中常用的尺寸单位
dp : 设备独立像素 - 用于设置边距和组件大小
sp :可伸缩像素 - 用于设置字体

默认定义尺寸文件 res/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="wordsize">18sp</dimen>
    <dimen name="margin">5dp</dimen>

</resources>

在xml中使用尺寸资源

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:gravity="center"
       android:textColor="@color/wordcolor"
       android:textSize="@dimen/wordsize"
       />

在java中使用哦尺寸资源

button.setTextSize(getResources().getDimension(R.dimen.small));

布局资源

layout布局文件就是布局资源

在java中设置布局资源

setContentView(R.layout.activity_main);

在布局文件中使用布局资源

先定义一个images布局管理器
在其他布局管理器中通过include引用images布局管理器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_1"
        android:text="@string/showme" />
    <include layout="@layout/images">
    </include>

</LinearLayout>

数组资源

定义资源数组

  • array : 定义尺寸资源
  • integer-array : 定义整形数组
  • string-array : 定义字符串数组
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="bgcolor">
        <item>0xBBE24A83</item>
        <item>0xBB318AD6</item>
        <item>0xBBD73943</item>
        <item>0xBBE69A08</item>
        <item>0xBBBD9663</item>
        <item>0xBBD45ABC</item>
        <item>0xBB4AA6D6</item>
        <item>0xBB8064D2</item>
        <item>0xBBF7A81E</item>
    </integer-array>
    <string-array name="word">
        <item>微信</item>
        <item>通讯录</item>
        <item>QQ</item>
        <item>相机</item>
        <item>时钟</item>
        <item>备忘录</item>
        <item>音乐</item>
        <item>互联网</item>
        <item>邮件</item>
    </string-array>
</resources>

在xml中的ListView显示数组资源的数据

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/showthis"
        android:entries="@array/word" />

在java中获取数组资源文件

 int[] color=getResources().getIntArray(R.array.bgcolor);		//获取保存背景颜色的数组
 String[] word=getResources().getStringArray(R.array.word);		//获取保存显示文字的数组

图片资源

Drawable资源

通常放置在res/drawable目录下,为了分辨率的不同,放置在不同的分辨率下

图片资源

  • png、jpg、gif、9.png(通过drwa9patch.bat来生成)

StateListDrawable资源
定义在XML文件中的Drawable对象,如按钮按下时变大
如编辑框,在获取焦点和不获取焦点时文字的颜色
1、在drawable目录下创建edittor.xml,设置获取焦点时失去焦点时的颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="#F60" />
    <item android:state_focused="false" android:color="#F0A0" />
</selector>

2、在xml中使用该StateListDrawable资源

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@drawable/edittor" />

mipmap资源

应用程序启动图标

主题和样式资源

主题资源

设置应用窗口的整体样子
默认在res/values/themes.xml文件中

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>
android:windowNoTitle 是否有标题栏
android:windowBackground 设置背景图片

在哪里使用使用主题资源
1、在AndroidManifest.xml
可以为整个app设置主题

android:theme="@style/Theme.MyApplication"

也可以为单个Activity设置主题

在Java中指定主题

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_MyApplication);
        setContentView(R.layout.activity_main); // 需要在这一行之前设置主题

样式资源

比如说文字样式
也是在res/values/themes.xml文件中中进行设置
主题:设置整个APP或窗口样式
样式:设置组件的样式

    <style name="title">
        <item name="android:textSize">30sp </item>
        <item name="android:textColor">#06F</item>
        
    </style>

在title样式的基础上进行修改

    <style name="title">
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#06F</item>
    </style>
    
    <style name="content" parent="title">
        <item name="android:textSize">10sp</item>
    </style>

使用样式资源

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/title"
        android:text="this abc"
        android:textColor="@drawable/edittor" />

菜单资源

菜单资源

在res目录下创建menu目录,再创建菜单资源文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/settings"
        android:title="@string/menu_title_settings"></item>
    <item
        android:id="@+id/regard"
        android:title="@string/menu_title_regard"></item>
</menu>

选项资源菜单

在java中使用菜单资源文件用作选项资源菜单

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = new MenuInflater(this);  //实例化一个MenuInflater对象
        menuInflater.inflate(R.menu.menu, menu);   //解析菜单文件
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) { //获取选中菜单id
            case R.id.settings: //通过选中id跳转指定页面
                Intent intent = new Intent(MainActivity.this, Settings.class);
                startActivity(intent);
                break;
            case R.id.regard:  //通过选中id跳转指定页面
                Intent intent1 = new Intent(MainActivity.this, Regard.class);
                startActivity(intent1);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

上下文菜单

在某个组件上长按时,创建的菜单
步骤:
1、为组件注册上下文菜单
2、添加上下文菜单
3、指定菜单项被选中时所应做出的处理

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_book_collect"
        android:title="@string/book_collect"></item>
    <item
        android:id="@+id/menu_book_report"
        android:title="@string/book_report"></item>
</menu>
public class MainActivity extends Activity {
    TextView introduce;  //定义TextView组件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        introduce = (TextView) findViewById(R.id.introduce);  //获取介绍TextView组件
        registerForContextMenu(introduce);        //为文本框注册上下文菜单
    }


    @Override
    //创建上下文菜单
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuInflater inflater = new MenuInflater(this); //实例化一个MenuInflater对象
            inflater.inflate(R.menu.introduce_menu, menu);    //解析菜单文件
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_copy: //选中介绍文字菜单中的复制时
                Toast.makeText(MainActivity.this, "已复制", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_collect: //选中介绍文字菜单中的收藏时
                Toast.makeText(MainActivity.this,"已收藏",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}

Android程序国际化

Internationalization - I18N
在这里插入图片描述
使用不同的values文件夹就可以了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《Android移动开发基础案例教程第二版》是一本非常实用的Android开发指南,提供了丰富的基础案例和教程,帮助读者提高Android开发技能。 该书配合源码使用,学习者可根据书中教程,从源码中学习如何使用Android SDK进行开发。源码中包含了丰富的示例代码,如UI控件基础、布局管理器、事件处理、Fragment碎片、Intent启动、ListView列表显示、GridView网格显示、RecyclerView列表及网格等常用控件使用方法,通过运行源码,读者可以更加清楚地理解这些知识点的实际应用场景。 同时,源码中也提供了完整的工程结构,读者可以从项目中学习如何搭建Android应用,如何进行资源文件的管理和使用等,为日后开发奠定了扎实的基础。 总之,《Android移动开发基础案例教程第二版》配合源码使用,可以帮助读者迅速掌握开发Android应用基础技能,在实践中积累经验,为日后的Android开发之路打下坚实的基础。 ### 回答2: 《Android移动开发基础案例教程(第二版)》是一本针对初学者编写的Android开发指南。本书的源码包含了许多实用案例,可以帮助读者更好地学习和掌握Android开发基础知识。 本书的源码总共包含了16个案例,其中包括了Activity、Intent、UI设计、布局、数据存储、网络通信等多个方面的内容。每个案例都循序渐进地展示了整个开发过程,从界面设计到逻辑代码的实现,再到测试和发布,全方位地让读者理解每一步。 此外,本书的源码也具有高精度、高效率、易扩展的特点,并且代码注释非常详细,使得读者可以快速了解每个函数和变量的作用,加深对代码的理解。 总体来说,《Android移动开发基础案例教程(第二版)》是一本优秀的Android开发入门指南,通过学习书中的源码,读者不仅可以熟悉Android开发基础知识,还可以掌握开发过程中的一些规范和最佳实践,从而更加高效地进行开发。 ### 回答3: 《Android移动开发基础案例教程 第二版》是一本介绍Android开发基础知识的教材,它通过不同的案例来讲解Android开发中常用的技术和工具。本书的第二版比第一版更新了更多的实例代码,同时更新了对Android Studio工具的介绍。 本书的代码清晰易懂,是初学者入门Android开发的好教材。该书源码可以在GitHub上下载,包含了本书中所有案例的完整代码和资源文件。学习者可以通过运行源码来快速了解案例的实现原理,并且可以自己进行修改和改进。 本书的案例涉及的知识点包括了Android界面设计、多媒体开发、网络编程等多个方面。通过学习这些案例,读者可以逐步掌握Android开发的关键技术,提升自己的编程能力。 总之,通过学习《Android移动开发基础案例教程 第二版》的源码,读者可以快速入门Android开发,掌握关键的技术和工具,同时也可以通过源码的改进和扩展来深入学习Android开发

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值