㈠ androidstudio中怎么使用imageview控件
在windows7操作系统下按照如下步骤使用imageView。
1、首先使用Android studio创建一个项目,项目结构如下图:
4、添加成功
㈡ android通知栏怎么添加控件
Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其他控件。否则会引起ClassNotFoundException异常
步骤如下:
1)创建自定义视图
2)获取远程视图对象(注:Notification的contentView不能为空)
3)设置PendingIntent(来响应各种事件)
4)发起Notification
大体4步骤这里就不详细说了,下面就把DEMO中的列子拿出来说下
样式:
1.自定义带按钮通知栏(如下样式)
正在进行的
“正在进行的”通知使用户了解正在运行的后台进程。例如,音乐播放器可以显示正在播放的音乐。也可以用来显示需要长时间处理的操作,例如下载或编码视频。“正在进行的”通知不能被手动删除。
实现方法如下:
java">实现方法如下:
/**
*带按钮的通知栏
*/
publicvoidshowButtonNotify(){
NotificationCompat.BuildermBuilder=newBuilder(this);
RemoteViewsmRemoteViews=newRemoteViews(getPackageName(),R.layout.view_custom_button);
mRemoteViews.setImageViewResource(R.id.custom_song_icon,R.drawable.sing_icon);
//API3.0以上的时候显示按钮,否则消失
mRemoteViews.setTextViewText(R.id.tv_custom_song_singer,"周杰伦");
mRemoteViews.setTextViewText(R.id.tv_custom_song_name,"七里香");
//如果版本号低于(3。0),那么不显示按钮
if(BaseTools.getSystemVersion()<=9){
mRemoteViews.setViewVisibility(R.id.ll_custom_button,View.GONE);
}else{
mRemoteViews.setViewVisibility(R.id.ll_custom_button,View.VISIBLE);
}
//
if(isPlay){
mRemoteViews.setImageViewResource(R.id.btn_custom_play,R.drawable.btn_pause);
}else{
mRemoteViews.setImageViewResource(R.id.btn_custom_play,R.drawable.btn_play);
}
//点击的事件处理
IntentbuttonIntent=newIntent(ACTION_BUTTON);
/*上一首按钮*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_PREV_ID);
//这里加了广播,所及INTENT的必须用getBroadcast方法
PendingIntentintent_prev=PendingIntent.getBroadcast(this,1,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev,intent_prev);
/*播放/暂停按钮*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_PALY_ID);
PendingIntentintent_paly=PendingIntent.getBroadcast(this,2,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play,intent_paly);
/*下一首按钮*/
buttonIntent.putExtra(INTENT_BUTTONID_TAG,BUTTON_NEXT_ID);
PendingIntentintent_next=PendingIntent.getBroadcast(this,3,buttonIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next,intent_next);
mBuilder.setContent(mRemoteViews)
.setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示
.setTicker("正在播放")
.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
.setOngoing(true)
.setSmallIcon(R.drawable.sing_icon);
Notificationnotify=mBuilder.build();
notify.flags=Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(notifyId,notify);
}
如果您对回答满意,请关注一下俺的微博
㈢ Android Studio控件设置样式怎么设置
Android Studio控件设置样式设置:
在windows7操作系统,Android studio中使用按照如下步骤设置Android Studio编辑中的代码字体的样式。
1、打开Android studio的设置界面,点击工具的扳手图标
2、在设置搜索栏输入"Font"
3、更改一下设置,“Primary font”是更改字体样式,比如“楷体”,“Size”我们可以更改字体的大小,“Line spacing”可以更改行间距
4、更改之后单击“Apply”应用更改,然后单击“OK”
㈣ binner android 控件怎么使用
读音如宾纳
㈤ 如何查看android studio 中的控件
可以通过将 WebBrowser (WebView)控件托管在应用程序中并使用其 Source 属性或 Navigate(Uri) 方法更改该控件的位置来实现该操作。 警告: 默认情况下,脚本在 WebBrowser 控件中处于禁用状态。如果您想在控件中启用脚本,请将 IsScriptEnabled 属性设置为 true。 以下代码示例显示如何从 ", UriKind", UriKind.Absolute); 或者,也可以使用 WebBrowser(WebView)类的 Navigate(Uri) 方法来实现该目标: Wp8: webBrowser1.Navigate(new Uri("", UriKind.Absolute)); Win8: webView.Navigate(new Uri("", UriKind.Absolute)); 如果您选择调用方法而不是设置属性,那么请记住,如果WebBrowser控件尚不在可视化树中,则会引发 InvalidOperationException。为了避免这个问题,您可以向 Loaded 事件附加一个处理程序,以确保在调用该方法之前此控件位于可视化树中。 .Loaded += (object sender, RoutedEventArgs e) => { webBrowser1.Navigate(new Uri("", UriKind.Absolute)); };webView1.Loaded += (object sender, RoutedEventArgs e) => { webView1.Navigate(new Uri("", UriKind.Absolute)); }; 2. 显示静态 Web 内容 您可以使用 WebBrowser (WebView)控件在应用程序中显示已设置格式的静态内容。例如,开发人员可能希望在应用程序包中包含帮助文本,以便用户可以随时访问。或者,您也可以使用 WebBrowser (WebView)控件显示应用程序已使用 SaveToString() 方法保存到独立存储的静态 Web 内容。 向项目中添加静态内容 向项目中添加静态内容的步骤 1. 使用以下 HTML 代码创建一个名为 readme.htm 的 HTML 文件: Sample Readme Content 2. 在 Visual Studio 中打开一个新的或现有的解决方案。 3. 在“解决方案资源管理器”中,右键单击您项目的名称,单击“添加”,然后单击“现有项”。 4. 导航到 readme.htm 文件的位置,选择该文件,然后单击“添加”。 5. 在“解决方案资源浏览器”中,单击该文件的名称。确认“属性”窗口中的“生成操作”部分。 添加命名空间 在页面后台代码中添加以下资源以包含以下命名空间。例如,如果您对主页使用默认命名约定,则应更新 MainPage.xaml.cs。 using System.IO.IsolatedStorage; using System.IO; using System.Windows.Resources; 添加 WebBrowser(WebView)控件 可以使用工具添加 WebBrowser (WebView)控件,也可以手动添加 WebBrowser (WebView)控件。 使用工具添加 WebBrowser(WebView)控件 使用工具添加 WebBrowser (WebView)控件的步骤 1. 在 Visual Studio 中打开一个新的或现有的解决方案。 2. 查看项目的 XAML 文件时,单击“工具箱”,将 WebBrowser (WebView)控件拖动到设备的图像中。 手动添加 WebBrowser(WebView)控件 在 XAML 中创建 WebBrowser (WebView)控件的步骤 1. 打开将在其中添加 WebBrowser (WebView)控件的页面的 XAML 文件。在“解决方案资源浏览器”中,右键单击该页面的 .xaml 文件(默认情况下,新应用程序的主页名为“MainPage.xaml”),然后选择“打开”。 2. 在 ContentGrid 中添加一个 WebBrowser (WebView)控件。例如: Wp8: Win8: 添加向独立存储中添加文件的代码 修改页面后台代码以包含以下两个函数,这两个函数将帮助向独立存储中添加静态文件。例如,如果您对主页使用默认命名约定,则应更新 MainPage.xaml.cs。 private void SaveFilesToIsoStore() { //These files must match what is included in the application package, //or BinaryStream.Dispose below will throw an exception. string[] files = { "readme.htm" }; IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); if (false == isoStore.FileExists(files[0])) { foreach (string f in files) { StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative)); using (BinaryReader br = new BinaryReader(sr.Stream)) { byte[] data = br.ReadBytes((int)sr.Stream.Length); SaveToIsoStore(f, data); } } } } private void SaveToIsoStore(string fileName, byte[] data) { string strBaseDir = string.Empty; string delimStr = "/"; char[] delimiter = delimStr.ToCharArray(); string[] dirsPath = fileName.Split(delimiter); //Get the IsoStore. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); //Re-create the directory structure. for (int i = 0; i < dirsPath.Length - 1; i++) { strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]); isoStore.CreateDirectory(strBaseDir); } //Remove the existing file. if (isoStore.FileExists(fileName)) { isoStore.DeleteFile(fileName); } //Write the file. using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName))) { bw.Write(data); bw.Close(); } }
㈥ Android中如何让控件居中
测试部报告锁屏界面里,设置锁屏的那个控件位置不好,有点偏下。以至于设置的时候,图标都在界外了。 打开packages/app/Settings里的res/layout-sw720dp/lockscreen_targets.xml 对于横屏和竖屏两种情况,其实应该定义两个目录一种叫做land,另一种叫做port(都是缩写),分别对应横屏和竖屏。 这里就增加一个目录layout-sw720dp-land/作为横屏的配置, layout-sw720dp为竖屏。 对于竖屏的情况 --- a/res/layout-sw720dp/lockscreen_targets.xml +++ b/res/layout-sw720dp/lockscreen_targets.xml @@ -26,7 +26,7 @@ android:layout_width=match_parent android:layout_height=0dp android:layout_weight=0.9 - android:gravity=left|top + android:gravity=top prvandroid:targetDrawables=@*android:array/lockscreen_targets_with_camera prvandroid:targetDescriptions=@*android:array/lockscreen_target_descriptions_with_camera prvandroid:directionDescriptions=@*android:array/lockscreen_direction_descriptions对于横屏将+ android:gravity=top改为+ android:gravity=left. 就可以居中显示了。 更保险的方法是
㈦ 请教android怎么让控件背景透明
以Android Studio为例,步骤如下:
1、直接打开相关窗口,在Android-app-res-layout的空白处点击鼠标右键并选择New-Layoutresource file。
㈧ android里如何在子线程中如何更新主线程的控件
步骤:
1、创建Handler对象(此处创建于主线程中便于更新UI)。
2、构建Runnable对象,在Runnable中更新界面。
3、在子线程的run方法中向UI线程post,runnable对象来更新UI。
代码
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button;
private TextView textview;
private final int SPLASH_DISPLAY_LENGHT = 1;
private static int flag = 0;
private static int count=0;
private int ab=1;
private boolean isrun = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView1);
final Handler handler= new Handler();
final Runnable runnable = new Runnable() {
public void run() {
textview.setText(Integer.toString(ab));
}
};
final Thread t = new Thread(){
//public boolean isrun=true;
@Override
public void run() {
while(isrun)
{
handler.post(runnable); //加入到消息队列 这样没有启动新的线程,虽然没有报异常。但仍然阻塞ProgressDialog的显示
ab++;
try {
sleep(1000); //直接调用
} catch (InterruptedException e) {
return;
}
}
}
};
t.start();
button.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
isrun=false;
}
});
}
}
㈨ 如何获取android安卓控件EditText中的内容
要获取EditText中的内容,要经过先定义控件-->指定id-->然后后在主类中绑定id-->然后就可以获取其中的内容了,步骤如下。 1. 在layout文件中定义EditText并为其指定id。如下图。 2.在类中定义EditText空间并绑定其id 3.通过下面的api或者EditText中的内容
㈩ android自定义控件怎么用
开发自定义控件的步骤:
1、了解View的工作原理
2、 编写继承自View的子类
3、 为自定义View类增加属性
4、 绘制控件
5、 响应用户消息
6 、自定义回调函数
一、View结构原理
Android系统的视图结构的设计也采用了组合模式,即View作为所有图形的基类,Viewgroup对View继承扩展为视图容器类。
View定义了绘图的基本操作
基本操作由三个函数完成:measure()、layout()、draw(),其内部又分别包含了onMeasure()、onLayout()、onDraw()三个子方法。具体操作如下:
1、measure操作
measure操作主要用于计算视图的大小,即视图的宽度和长度。在view中定义为final类型,要求子类不能修改。measure()函数中又会调用下面的函数:
(1)onMeasure(),视图大小的将在这里最终确定,也就是说measure只是对onMeasure的一个包装,子类可以覆写onMeasure()方法实现自己的计算视图大小的方式,并通过setMeasuredDimension(width, height)保存计算结果。
2、layout操作
layout操作用于设置视图在屏幕中显示的位置。在view中定义为final类型,要求子类不能修改。layout()函数中有两个基本操作:
(1)setFrame(l,t,r,b),l,t,r,b即子视图在父视图中的具体位置,该函数用于将这些参数保存起来;
(2)onLayout(),在View中这个函数什么都不会做,提供该函数主要是为viewGroup类型布局子视图用的;
3、draw操作
draw操作利用前两部得到的参数,将视图显示在屏幕上,到这里也就完成了整个的视图绘制工作。子类也不应该修改该方法,因为其内部定义了绘图的基本操作:
(1)绘制背景;
(2)如果要视图显示渐变框,这里会做一些准备工作;
(3)绘制视图本身,即调用onDraw()函数。在view中onDraw()是个空函数,也就是说具体的视图都要覆写该函数来实现自己的显示(比如TextView在这里实现了绘制文字的过程)。而对于ViewGroup则不需要实现该函数,因为作为容器是“没有内容“的,其包含了多个子view,而子View已经实现了自己的绘制方法,因此只需要告诉子view绘制自己就可以了,也就是下面的dispatchDraw()方法;
(4)绘制子视图,即dispatchDraw()函数。在view中这是个空函数,具体的视图不需要实现该方法,它是专门为容器类准备的,也就是容器类必须实现该方法;
(5)如果需要(应用程序调用了setVerticalFadingEdge或者setHorizontalFadingEdge),开始绘制渐变框;
(6)绘制滚动条;
从上面可以看出自定义View需要最少覆写onMeasure()和onDraw()两个方法。
二、View类的构造方法
创建自定义控件的3种主要实现方式:
1)继承已有的控件来实现自定义控件: 主要是当要实现的控件和已有的控件在很多方面比较类似, 通过对已有控件的扩展来满足要求。
2)通过继承一个布局文件实现自定义控件,一般来说做组合控件时可以通过这个方式来实现。
注意此时不用onDraw方法,在构造广告中通过inflater加载自定义控件的布局文件,再addView(view),自定义控件的图形界面就加载进来了。
3)通过继承view类来实现自定义控件,使用GDI绘制出组件界面,一般无法通过上述两种方式来实现时用该方式。
三、自定义View增加属性的两种方法:
1)在View类中定义。通过构造函数中引入的AttributeSet 去查找XML布局的属性名称,然后找到它对应引用的资源ID去找值。
案例:实现一个带文字的图片(图片、文字是onDraw方法重绘实现)
public class MyView extends View {
private String mtext;
private int msrc;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
int resourceId = 0;
int textId = attrs.getAttributeResourceValue(null, "Text",0);
int srcId = attrs.getAttributeResourceValue(null, "Src", 0);
mtext = context.getResources().getText(textId).toString();
msrc = srcId;
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.RED);
InputStream is = getResources().openRawResource(msrc);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
int bh = mBitmap.getHeight();
int bw = mBitmap.getWidth();
canvas.drawBitmap(mBitmap, 0,0, paint);
//canvas.drawCircle(40, 90, 15, paint);
canvas.drawText(mtext, bw/2, 30, paint);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.myimageview2.MyView
android:id="@+id/myView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Text="@string/hello_world"
Src="@drawable/xh"/>
</LinearLayout>
属性Text, Src在自定义View类的构造方法中读取。
2)通过XML为View注册属性。与Android提供的标准属性写法一样。
案例: 实现一个带文字说明的ImageView (ImageView+TextView组合,文字说明,可在布局文件中设置位置)
public class MyImageView extends LinearLayout {
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
int resourceId = -1;
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.MyImageView);
ImageView iv = new ImageView(context);
TextView tv = new TextView(context);
int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.MyImageView_Oriental:
resourceId = typedArray.getInt(
R.styleable.MyImageView_Oriental, 0);
this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL
: LinearLayout.VERTICAL);
break;
case R.styleable.MyImageView_Text:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Text, 0);
tv.setText(resourceId > 0 ? typedArray.getResources().getText(
resourceId) : typedArray
.getString(R.styleable.MyImageView_Text));
break;
case R.styleable.MyImageView_Src:
resourceId = typedArray.getResourceId(
R.styleable.MyImageView_Src, 0);
iv.setImageResource(resourceId > 0 ?resourceId:R.drawable.ic_launcher);
break;
}
}
addView(iv);
addView(tv);
typedArray.recycle();
}
}
attrs.xml进行属性声明, 文件放在values目录下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyImageView">
<attr name="Text" format="reference|string"></attr>
<attr name="Oriental" >
<enum name="Horizontal" value="1"></enum>
<enum name="Vertical" value="0"></enum>
</attr>
<attr name="Src" format="reference|integer"></attr>
</declare-styleable>
</resources>
MainActivity的布局文件:先定义命名空间 xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2" (com.example.myimageview2为你
在manifest中定义的包名)
然后可以像使用系统的属性一样使用:uview:Oriental="Vertical"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:uview="http://schemas.android.com/apk/res/com.example.myimageview2"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.example.myimageview2.MyImageView
android:id="@+id/myImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
uview:Text="这是一个图片说明"
uview:Src="@drawable/tw"
uview:Oriental="Vertical">
</com.example.myimageview2.MyImageView>
</LinearLayout>
四、控件绘制 onDraw()
五、
六:自定义View的方法
onFinishInflate() 回调方法,当应用从XML加载该组件并用它构建界面之后调用的方法
onMeasure() 检测View组件及其子组件的大小
onLayout() 当该组件需要分配其子组件的位置、大小时
onSizeChange() 当该组件的大小被改变时
onDraw() 当组件将要绘制它的内容时
onKeyDown 当按下某个键盘时
onKeyUp 当松开某个键盘时
onTrackballEvent 当发生轨迹球事件时
onTouchEvent 当发生触屏事件时
onWindowFocusChanged(boolean) 当该组件得到、失去焦点时
onAtrrachedToWindow() 当把该组件放入到某个窗口时
onDetachedFromWindow() 当把该组件从某个窗口上分离时触发的方法
onWindowVisibilityChanged(int): 当包含该组件的窗口的可见性发生改变时触发的方法