㈠ 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): 當包含該組件的窗口的可見性發生改變時觸發的方法