设置控件的颜色,字体
2009-11-23 21:08
113 查看
代码:
package com.oyzz.ch3_6;
import android.app.Activity;
/*必须引用graphics.Color才能使用Color.*的对象*/
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
/*必须引用 widget.Button才能声明使用Button对象*/
import android.widget.Button;
/*必须引用 widget.TextView才能声明使用TestView对象*/
import android.widget.TextView;
public class Ch3_6 extends Activity
{
private Button mButton;
private TextView mText;
private int[] mColors;
private int colornum;
private Button fontButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*通过findViewById构造器来使用main.xml与string.xml
中button与textView的参数*/
mButton=(Button) findViewById(R.id.mybutton);
mText= (TextView) findViewById(R.id.mytext);
fontButton=(Button) findViewById(R.id.mybutton1);
/*声明并构造一整数array来存储欲使用的文字颜色*/
mColors = new int[]
{
Color.BLACK, Color.RED, Color.BLUE,
Color.GREEN, Color.MAGENTA, Color.YELLOW
};
colornum=0;
//得到color.xml文件里的颜色
int b = getResources().getColor(R.drawable.blue);//得到配置文件里的颜色
mButton.setTextColor(b);
/*使用setOnClickListener让按钮聆听事件*/
mButton.setOnClickListener(new View.OnClickListener()
{
/*使用onClick让用户点下按钮来驱动变动文字颜色*/
public void onClick(View v)
{
if (colornum < mColors.length)
{
mText.setTextColor(mColors[colornum]);
colornum++;
}
else
colornum=0;
}
});
fontButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//设置字体
fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用内部支持的方式设置
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout使用白色的背景 -->
<LinearLayout
android:id="@+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<!--
文字使用mytext作為id使用string.xml中
的textview_str參數 預設文字顏色為按灰色
-->
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview_str"
android:textColor="@drawable/darkgray"
>
</TextView>
<!-- 按鈕以mybutton作為id使用string.xml中
的button_str參數
-->
<Button
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_str"
>
</Button>
<Button
android:id="@+id/mybutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="字体"
>
</Button>
</LinearLayout>
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="darkgray">#404040ff</drawable>
<drawable name="black">#000</drawable>
<drawable name="red">#ff00ff</drawable>
<drawable name="green">#0ff0ff</drawable>
<drawable name="lightgray">#c0c0c0ff</drawable>
<drawable name="white">#ffffffff</drawable>
<drawable name="yellow">#ffFF33ff</drawable>
<drawable name="blue">#00ffff</drawable>
<drawable name="gray">#808080ff</drawable>
<drawable name="magenta">#ff6699ff</drawable>
<drawable name="cyan">#66ffffff</drawable>
</resources>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex03_13</string>
<string name="app_name">Ex03_13</string>
<string name="textview_str">转吧七彩霓虹灯</string>
<string name="button_str">按我</string>
</resources>
体会:
1.用代码设置控件的颜色:
int b = getResources().getColor(R.drawable.blue);//得到配置文件里的颜色
mButton.setTextColor(b);
2.设置空间的字体:
方式一:mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//设置字体
注意:1.保证文件一定是ttf格式;2.放到assets/fonts目录下;3.如果找不到相应的字体不会报错,只是在运行的时候显示不出来
方式二: fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用内部支持的方式设置
package com.oyzz.ch3_6;
import android.app.Activity;
/*必须引用graphics.Color才能使用Color.*的对象*/
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
/*必须引用 widget.Button才能声明使用Button对象*/
import android.widget.Button;
/*必须引用 widget.TextView才能声明使用TestView对象*/
import android.widget.TextView;
public class Ch3_6 extends Activity
{
private Button mButton;
private TextView mText;
private int[] mColors;
private int colornum;
private Button fontButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*通过findViewById构造器来使用main.xml与string.xml
中button与textView的参数*/
mButton=(Button) findViewById(R.id.mybutton);
mText= (TextView) findViewById(R.id.mytext);
fontButton=(Button) findViewById(R.id.mybutton1);
/*声明并构造一整数array来存储欲使用的文字颜色*/
mColors = new int[]
{
Color.BLACK, Color.RED, Color.BLUE,
Color.GREEN, Color.MAGENTA, Color.YELLOW
};
colornum=0;
//得到color.xml文件里的颜色
int b = getResources().getColor(R.drawable.blue);//得到配置文件里的颜色
mButton.setTextColor(b);
/*使用setOnClickListener让按钮聆听事件*/
mButton.setOnClickListener(new View.OnClickListener()
{
/*使用onClick让用户点下按钮来驱动变动文字颜色*/
public void onClick(View v)
{
if (colornum < mColors.length)
{
mText.setTextColor(mColors[colornum]);
colornum++;
}
else
colornum=0;
}
});
fontButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//设置字体
fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用内部支持的方式设置
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout使用白色的背景 -->
<LinearLayout
android:id="@+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<!--
文字使用mytext作為id使用string.xml中
的textview_str參數 預設文字顏色為按灰色
-->
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview_str"
android:textColor="@drawable/darkgray"
>
</TextView>
<!-- 按鈕以mybutton作為id使用string.xml中
的button_str參數
-->
<Button
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_str"
>
</Button>
<Button
android:id="@+id/mybutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="字体"
>
</Button>
</LinearLayout>
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="darkgray">#404040ff</drawable>
<drawable name="black">#000</drawable>
<drawable name="red">#ff00ff</drawable>
<drawable name="green">#0ff0ff</drawable>
<drawable name="lightgray">#c0c0c0ff</drawable>
<drawable name="white">#ffffffff</drawable>
<drawable name="yellow">#ffFF33ff</drawable>
<drawable name="blue">#00ffff</drawable>
<drawable name="gray">#808080ff</drawable>
<drawable name="magenta">#ff6699ff</drawable>
<drawable name="cyan">#66ffffff</drawable>
</resources>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex03_13</string>
<string name="app_name">Ex03_13</string>
<string name="textview_str">转吧七彩霓虹灯</string>
<string name="button_str">按我</string>
</resources>
体会:
1.用代码设置控件的颜色:
int b = getResources().getColor(R.drawable.blue);//得到配置文件里的颜色
mButton.setTextColor(b);
2.设置空间的字体:
方式一:mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//设置字体
注意:1.保证文件一定是ttf格式;2.放到assets/fonts目录下;3.如果找不到相应的字体不会报错,只是在运行的时候显示不出来
方式二: fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用内部支持的方式设置
相关文章推荐
- [转]对话框与其控件的颜色,字体的设置
- 关于设置对话框的背景颜色,和设置控件的字体颜色或背景颜色
- OnCtlColor函数:MFC设置静态文本框,编辑框等控件背景和字体颜色
- OnCtlColor函数:MFC设置静态文本框,编辑框等控件背景和字体颜色
- MFC 可以设置背景色、字体、字体颜色、透明背景的 Static 静态文本控件
- MFC窗口各控件的颜色和字体的设置
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- android 设置控件的颜色字体的方法
- VC设置控件字体颜色
- VC设置控件的字体颜色和背景颜色和字体大小和样式
- 设置EditText控件中提示消息hint的字体颜色和大小
- 控件字体及颜色设置
- 设置NumericStepper控件不可用状态的字体颜色。
- VC中控件字体与文本颜色的设置
- 控件设置Enable改变字体颜色
- 动态设置Edit控件的背景及字体颜色
- MFC窗口各控件的颜色和字体的设置【转】
- OnCtlColor函数:MFC设置静态文本框,编辑框等控件背景和字体颜色
- OnCtlColor函数:MFC设置静态文本框,编辑框等控件背景和字体颜色
- VC、MFC中设置控件的背景色、标题、字体颜色、字体要注意的地方[转]