您的位置:首页 > 移动开发 > Android开发

android中的用户资源访问(一)

2014-12-13 00:00 246 查看
这几天要总结一下android开发中的用户资源访问。

android中的用户资源存在项目工程中res文件夹下,有字符串、颜色、大小、数组、布局、样式、主题等资源,这些资源可以在xml文件中引用,也可以在android源码文件中使用,今天总结一下字符串、颜色、大小、数组、布局和图片资源。

总的来说,在xml文件中引用的格式为[<package>.]@/XXX/name;在源码中引用格式是[<package>.]R.XXX.name。

先贴上在xml文件中引用的代码(在布局xml文件中)和在源码中引用代码。

//main.xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="main.testresources.MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/lor"
android:textSize="@dimen/dim"
android:text="@string/tv1"
android:background="@drawable/ic_launcher" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"
android:textColor="@color/lor2"
android:textSize="@dimen/dim2"
android:background="@drawable/ic_launcher"/>
<!-- 在ADT 16.0以上,在一些没有文本显示的控件里,需要加上功能描述,即 android:contentDescription="@string/jieshao";android:src="@drawable/test"是设置图片显示 -->
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/jieshao"
android:src="@drawable/test"/>
<!-- 引用外部布局文件,这样会将外部布局文件添加到该布局文件中,成为该布局的一部分,布局文件名称layout_test.xml-->
<include layout="@layout/layout_test" />

</LinearLayout>

//layout_test.xml布局文件,供main.xml文件调用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button2" />
</LinearLayout>

//android源码文件
public class MainActivity extends ActionBarActivity implements OnClickListener{

protected TextView tv1=null;
protected Button btn1=null;
protected int num=0;
protected StringBuffer stringBuffer=null;
protected String[] arr=new String[3];
protected String str=null;
protected int col=0;
protected float dim=0;
protected ImageView iv=null;

@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//引用布局文件,并加载显示

tv1=(TextView)findViewById(R.id.tv1);
btn1=(Button)findViewById(R.id.btn1);
iv=(ImageView)findViewById(R.id.iv1);

str=getResources().getString(R.string.tv1);//取得字符串资源
tv1.setText(str);

col=getResources().getColor(R.color.lor);//取得颜色资源
btn1.setTextColor(col);//设置文字显示颜色
btn1.setBackgroundColor(col);//设置按钮背景颜色

dim=getResources().getDimension(R.dimen.dim);//取得大小资源
btn1.setTextSize(dim);//设置显示文字大小

arr=getResources().getStringArray(R.array.world);//取得数组资源
btn1.setText(arr[0]);//取数组中的第一个元素作为按钮显示文本

tv1.setBackground(getResources().getDrawable(R.drawable.ic_launcher));//取得图片资源,并设置成背景图片
iv.setImageResource(R.drawable.test);//设置显示图片

btn1.setOnClickListener(MainActivity.this);
}

(1)、字符串资源

存在位置:/res/values/strings.xml,根元素是<resources></resources>,用<string></string>标记,如下代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TestResources</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="tv1">第一个</string>
<string name="btn1">点我切换显示</string>
</resources>

(2)、颜色资源

存在位置:/res/values/colors.xml,根元素为<resources></resources>,用<color></color>标记,如下代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 半透明,红色 -->
<color name="lor">#66ff0000</color>
<!-- 半透明,绿色 -->
<color name="lor2">#6600ff00</color>
</resources>

(3)、大小资源

存在位置:/res/values/dimens.xml,根元素为<resources></resources>,用<dimen></dimen>标记,如下代码

<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="dim">30dp</dimen>
<dimen name="dim2">40dp</dimen>
</resources>

(4)、数组资源

存在位置:/res/values/arrays.xml,根元素为<resources></resources>,用<integer-array></integer-array>或<string-array></string-array>标记,如下代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="hello">
<item >1</item>
<item >2</item>
<item >3</item>
</integer-array>
<string-array name="world">
<item >one</item>
<item >two</item>
<item >three</item>
</string-array>
</resources>

(5)、布局文件资源

存在位置:/res/layout/xxx.xml,根元素不定,一般是<LinearLayout></LinearLayout>,用各个组件节点标记,见上述贴出的布局xml文件

(6)、图片资源

存在位置:/res/drawable-xxxx/图片名,非xml文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: