Android學習小記(二)UI (開發工具:Android Studio)
UI
一、 Android中常用控制元件的使用方法
1. TextView
TextView常用指定程式碼如下所示:
1、android:text 設定文字的內容
2、android:textColor設定文字的顏色
3、android:textSize設定文字的字型大小(一般使用sp)
4、android:height 設定文字的高度(一般使用dp)
5、android:width 設定文字的寬度(一般使用dp)
6、android:inputType設定文字的型別,預設為普通文字,可選
7、android:ems 設定textView的寬度為N個字元的寬度
8、android:gravity設定文字框的內容相對於文字框的位置(可以使用多個屬性中間用 “ | ”分割)
9、android:drawableLeft用於在文字框左側繪製圖片
10、android:drawableRight用於在文字框右側繪製圖片
11、android:drawableTop用於在文字框頂部繪製圖片
12、android:drawableBottom用於在文字框底部繪製圖片
13、android:hint 設定預設顯示字型
14、android:textStyle
15、android:ellipsize設定當文字過長的時候該控制元件如何顯示。
16、android:maxLength:限制文字的長度,超出部分將會不顯示
17、android:lines 設定顯示的行數,即使沒有資料也會顯示
18、android:singleLine設定文字是否單行顯示
19、android:clickable把其屬性更改為true,為textView設定事件攔截
網傳跑馬燈效果的low版實現:
首先更改佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--首先設定佈局為相對佈局-->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:text="跑馬燈效果,多撈噢!" />
<!--第一行指定Text id標識不多說了,與Button類似,第二、三行指定寬度與高度剛好包含的
了Text即可,第四行指定了字號大小,第五行用於設定當行顯示,第六行是重點ellipize設
置為marquee就是跑馬燈屬性,第七行marqueeReatLimit設定marquee_forever的意思
為一直都是滾動模式,第八行指定了需要跑馬燈效果的Text內容。
-->
</RelativeLayout>
其次一定要在Activity裡面設定一個屬性tv.setSelected(true); 不設定這個屬性,字型不會開始滾動,Activity指令如下:
package activitytext.example.com.uiwidgettext;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView);
tv.setSelected(true);
}
}
效果如圖:會自動滾動
2.Button
Button的可使用的配置與TextView差不多,我們可以在佈局檔案裡這樣加入Button:
<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" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:text="Button"/></LinearLayout>
小插曲:佈局裡面設定的檔案是“Button"但是最終顯示的結果是"BUTTON"因為系統會把Button裡所有的英文字母自動進行大寫轉換,要取消這一效果可以在<Button/>中加入一句:
android:textAllCaps="false"/>
Button監聽的基本方法的實現:
方式一:匿名類方式。在MainActivity為Button註冊一個監聽器:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button =(Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //在此新增邏輯(你想要實現的什麼)} } );} //點選按鈕時,會執行監聽器中onClick()方法 }
方式二:實現介面的方式。使用實現介面的方式進行註冊:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: //在此新增邏輯 break; default: break; } } }
3.EditText
EditText在前面並沒有怎麼接觸到。EditText是程式用於和使用者進行互動的另一個重要控制元件,它允許使用者在控制元件裡輸入和編輯內容,並可以在程式中對這些內容進行處理。
首先現在佈局檔案中加入EditText
<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" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit_text"/></LinearLayout>
在輸入框中加入提示性文字(一旦使用者輸入任何內容,提示性的文字就會消失):即在EditText中加入一個屬性:
<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" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit_text" android:hint="Type something Please!"/></LinearLayout>
執行效果如圖:使用者可在長下劃線中編輯文字。
當隨著輸入的內容不斷增多,EditText會不斷拉長,因為高度是wrap_content,因此總能包含主裡面的內容,當輸入內容過多時會非常的醜。我們可以加入android:maxlines這個屬性來解決這個問題:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit_text" android:hint="Type something Please!" android:maxLines="2"/> <!--這裡設定了EditText的最大長度為兩行,這樣輸入的內容超過兩行後, 文字就會自動向上滾動,則不會繼續拉伸,使用者可以上下滑動EditText 來瀏覽全部內容。-->
執行效果如圖:
我們還可以結合EditText和Button與Toast來進行一些操作,比如點選按鈕來獲取EditText中輸入的內容:
首先修改ManiActivity中的程式碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button) findViewById(R.id.button); editText=(EditText)findViewById(R.id.edit_text);button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: String inputText=editText.getText().toString(); Toast.makeText(MainActivity.this,inputText, Toast.LENGTH_SHORT).show(); break; default: break; } } } /* //首先通過findViewById()方法得到EditText的例項,然後在按鈕的點選事件裡呼叫EditText的getText()方法 來獲取到輸入的內容,再呼叫toString()方法轉換成字串,最後使用Toast將獲取的內容顯示出來*/
執行效果如圖:
4.ImageView
ImageView是用於在介面上展示圖片的一個控制元件、使用時我們首先要再佈局檔案裡定義一個ImageView:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image_view" android:src="@drawable/img_1"/> //可以看到這裡通過android:src屬性給ImageView指定了一張圖片。由於圖片寬度高度未知 ,所以width和height都設定為wrap_content </LinearLayout>
我們還可以通過程式碼動態地更改ImageView中的圖片,比如點選一個Button使其變成另外一張圖片,在MainActivity修改如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editText; private ImageView imageView;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button) findViewById(R.id.button); editText=(EditText)findViewById(R.id.edit_text); imageView=(ImageView)findViewById(R.id.image_view);button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: imageView.setImageResource(R.drawable.img_2);String inputText=editText.getText().toString(); Toast.makeText(MainActivity.this,inputText, Toast.LENGTH_SHORT).show(); break; default: break; } } }
效果如下圖:
點選按鈕以後變為:
小插曲:由於AS一般會自動使用的佈局是RelativeLayout,即相對佈局. 此時需要把RelativeLayout改為LinearLayout .直接修改它會報錯。
Wrong orientation? No orientation specified, and the default is horizontal, yet thislayout has multiple children where at least one haslayout_width="match_parent",並且我們Button充滿了整個螢幕,ImageView、EditText都被覆蓋了。
解決方法:
通常發生這個錯誤提示的原因是我們直接在原有的頁面上把別的佈局標籤改成<LinearLayout>,但是使用<LinearLayout>標籤要指明方向,水平方向還是垂直方向
android:orientation="horizontal"
有人會問當用了android:orientation="horizontal"執行之後介面只顯示一個<Textview>而自己寫了4個<Textview>這是因為它的屬性是為垂直線性佈局的,所以只有一個顯示。
解決辦法:
改為下面的佈局
android:orientation = "vertical"
5.progressBar
ProgressBar是一種進度條元件,通常向使用者展示某個耗時操作完成的進度,而不讓使用者覺得是程式失去了相應,從而更好的提升了使用者介面的友好性。
它的用法和以上控制元件及其相似,首先先修改activity_main.xml中的程式碼:
<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progress_bar"/>
執行後會發現有一個圓形進度條正在旋轉:
旋轉的進度條表示我們的程式正在載入資料,資料總是有載入完的時候,這個時候進度條就應該消失,不應該一直旋轉下去了,要實現它需要用到android:visibility這個屬性進行指定,可選值有三個,分別是:visible、invisible、gone。visible表示這個控制元件式可見的,這個也是一般控制元件的預設值,invisible表示控制元件不可見,但它仍佔著原來的位置和大小,gone表示控制元件不僅不可見,而且不佔用任何螢幕空間。我們還可以用程式碼實現控制元件的可見性,使用的是setVisibility()可以傳入View.VISIBLE、View.INVISIBLE、View.GONE這三種值。為了測試我們來實現點選按鈕讓進度條出現和消失這種效果,先修改MainActivity中的程式碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editText; private ImageView imageView; private ProgressBar progressBar;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button) findViewById(R.id.button); editText=(EditText)findViewById(R.id.edit_text); imageView=(ImageView)findViewById(R.id.image_view); progressBar=(ProgressBar)findViewById(R.id.progress_bar);button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: if(progressBar.getVisibility()==View.GONE){ progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.GONE); }break; default: break; } } } /*在按鈕的點選事件中,我們通過getVisibility()方法來判斷 ProgressBar是否可見,如果可見就將ProgressBar隱藏掉, 如果不可見就將ProgressBar顯示出來*/
我們還可以給ProgressBar指定不同的央視,通過style屬性將它換成水平進度條,修改activity_main.xml中的程式碼:
<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progress_bar" style="?android:attr/progressBarStyleHorizontal"/>執行後:發現已經由圓形進度條變為水平進度條了。
換為水平進度條後,可以通過android:max屬性給進度條設定一個最大值,然後在程式碼中動態地更改進度條的進度,修改MainActivity中的程式碼:
@Override public void onClick(View v) { switch (v.getId()){ case R.id.button: int progress=progressBar.getProgress(); progress=progress+10; progressBar.setProgress(progress); break; default: break; } } }
每點選一次按鈕就會在現有的進度上加10作為更新後的進度。點選數次按鈕後的效果如圖:
6.AlertDialog
AlertDialog可以在當前的介面彈出一個對話方塊,且這個對話方塊是置頂於所有介面元素之上的,能夠遮蔽掉其他控制元件的互動能力,因此AlertDialog一般都是用於提示一些非常重要的內容或者警告資訊,下面我們就來練練手,首先先修改ManiActivity中的程式碼:
@Override public void onClick(View v) { switch (v.getId()){ case R.id.button: AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this); dialog.setTitle("This is Dialog"); dialog.setMessage("Something Important."); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.show(); break; default: break; } } } /*首先通過AlertDialog.Builder建立一個AlertDialog的例項,然後可以為這個對話方塊 設定標題、內容、可否用Back鍵關閉對話方塊等屬性,接下來呼叫setPositiveButton() 方法來為對話方塊確定按鈕的點選事件,呼叫setNegativeButton()方法設定取消按鈕點選事件 最後呼叫show()方法將對話方塊顯示出來*/
執行後效果如圖:
7.ProgressDialog
ProgressDialog與AlertDialog類似,都可以在介面上彈出一個對話方塊,都能夠遮蔽掉其他控制元件的互動能力,不同的是,ProgressDialog會在對話方塊顯示一個進度條,一般用於表示當前操作比較耗時,讓使用者耐心地等待。它的用法與AlertDialog也比較類似,修改MainActivity中的程式碼:
@Override public void onClick(View v) { switch (v.getId()){ case R.id.button: ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); progressDialog.setTitle("This is ProgressDialog"); ProgressDialog.setMessage("Loading....."); progressDialog.setCancelable(true); progressDialog.show(); break; default: break; } } } /*首先先構建出一個ProgressDialog物件,然後同樣可以設定標題、可否取消等屬性, 最後也是通過呼叫show()方法將ProgressDialog顯示出來*/
執行效果如圖:
注:如果在setCancelable()中傳入了false,表示ProgressDialo不能通過Back鍵取消掉的,這個時候需要在資料完成後呼叫ProgressDialog的dismiss()方法來關閉對話方塊,否則ProgressDialog會一直存在
二、四種基本佈局(本因有五種基本佈局,但是AbsoluteLayout(絕對佈局)已經基本上被禁用了,因此就不考慮此了)
1.線性佈局
LinearLayout又稱作線性佈局,是一種非常常用的佈局。通過android:orientation屬性指定了排列方向是vertical,如果指定的是horizontal,控制元件就會在水平方向上排列了。首先修改activity_main.xml 這裡我們建立了三個Button.並指定orientation即排列方式是垂直的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3"/> </LinearLayout>執行後的結果如圖:
之後我們修改排列方式為水平(horizontal)後,執行的效果如圖:
注:如果LinearLayout的排列方向是horizontal,內部的控制元件就絕對不能將寬度指定為match_parent,因為這樣的話,單獨一個控制元件就會將整個水平方向佔滿,其他的控制元件就沒有可放置的位置了。同樣的道理,如果LinearLayout的排列方向是vertical,內部的控制元件就不能將高度指定為match_parent。
接下來是瞭解android:layout_gravity屬性,android:gravity用於指定文字在佈局中的對齊方式。而layout_gravity用於指定控制元件在佈局中的對齊方式。注意的是,當LinearLayout的排列方向是horizontal時,只有垂直方向上的對齊方式才會生效,因為此時水平向上的長度是不固定的,每新增一個控制元件,水平方向上的長度都會改變,因而無法指定該方向上的對齊方式。同理,當LinearLayout的排列方向是vertical時,只有水平方向上的對齊方式才會生效。同樣修改activity.main.xml中的程式碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top"android:text="Button1"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical"android:text="Button2"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom"android:text="Button3"/> </LinearLayout>
執行後的結果如圖:
LinearLayout的另外一個重要屬性——android:layout_weight.這個屬性允許我們使用比例的方式來指定控制元件的大小,它在手機螢幕的適配性方面可以起到非常重要的作用。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/input_message" android:layout_width="0dp" android:layout_weight="1"android:layout_height="wrap_content" android:hint="Type something" />相關推薦
Android學習小記(二)UI (開發工具:Android Studio)
UI一、 Android中常用控制元件的使用方法1. TextViewTextView常用指定程式碼如下所示:1、android:te
Android學習心得(二)
Android儲存方式 儲存偏好設定 持久化資料:使應用程式執行時需要長期儲存一些資料。 SharePreferences物件、檔案和 SQLite資料庫來儲存這些持久化資料 存取活動的偏好設定 SharePreferences類屬於Android.cntent
利用tensorflow實現簡單的卷積神經網路——遷移學習小記(二)
一、什麼是神經網路(CNN) 卷積神經網路(Convolutional Neural Network,簡稱CNN),是一種前饋神經網路,人工神經元可以影響周圍單元,可以進行大型影象處理。卷積神經網路包括卷積層和池化層。卷積神經網路是受到生物思考方式的啟發的MLPs(多
Android學習筆記(二)
1.Android的APK瘦身方法 在這裡給大家推薦一個網站,關於圖片線上壓縮的TinyPNG,然後使用android studio自帶工具Lint剔除無用資源(res資原始檔),如果你閒的話那就再手
Android學習碎片(二)——如何從本地圖片獲得Bitmap例項
準備學習Bitmap相關知識,於是先學習下怎麼從本地圖片獲得Bitmap例項。 第一種方法: //獲取本地圖片Bitmap例項的第一種方法 Resources resources
Pro Android學習筆記(二)——ContentProvider(下)
上一章講解了ContentProvider的一些基本概念和涉及到的知識。這一章就來實現它。 要實現ContentProvider,我們繼承自ContentProvider這個抽象類,實現其中的抽象方法就可以了,其中的抽象方法包括: query insert update delete g
某宅的Android學習筆記(二)——圖片三級快取
圖片三級快取的重要性 很多時候我們都需要從網路上下載圖片,如果在圖片很多的情況下,每次啟動app都要從網上下載,就會造成流量的浪費,影響使用者的體驗。因此,要利用快取來避免圖片的重複載入。 圖片快取方式 所謂三級快取,即: 網路快取 記憶體快取
Android學習筆記(二)--ViewPager的使用(輪播功能的實現)
是否覺得只有幾個按鈕和幾個View有點太單一呢,頁面上怎麼能只顯示這麼少的內容呢?沒關係,ViewPager來幫忙。(聽說已經過時,但還是看一下吧,總是相通的)這裡是參考了《第一行程式碼》一書。 先上程式碼。 MainActivity.java pac
Android學習心得(二) 關於getContext()
2017.8.19 16:44 《第一行程式碼》學習中 Q:在編寫一個天氣APP程式碼的時候,出了一個問題:直接在程式碼中呼叫getContext()。 結果,報錯No virtual method
C#基礎總結(二) —— C#開發工具 Visual Studio(IDE)
暫停 基本上 必備 img 包含 adl 裏的 方案 運行 一、Visual Studio Visual Studio 是微軟公司的一個開發工具集,是C#開發必備利器。下面附上VS2013簡體中文社區版的下載地址: 鏈接:https://pan.baidu.com
《自己動手寫java虛擬機器》學習筆記(二)-----命令列工具(java)
專案地址:https://github.com/gongxianshengjiadexiaohuihui 首先是Cmd的類 /** * @ClassName Cmd * @Description TODO * @Author Mr.G * @Date 2018/10/9 9:40
Android學習路線(二十一)運用Fragment構建動態UI——創建一個Fragment
動態 app idt 文檔 部分 roi 現實 調用 android學習 你能夠把fragment看成是activity的模塊化部分。它擁有自己的生命周期,接受它自己的輸入事件,你能夠在activity執行時加入或者刪除它(有點像是一個“子activity”。你
安卓開發學習筆記(二):Android Stuidio無法引用Intent來創建對象,出現cannot resolve xxx
編譯器 port stact 消失 click first 紅色 xxx font 筆者在進行安卓開發時,發現自己的代碼語法完全沒有問題。尤其是創建intent對象的時候,語法完全是正確的,但是Android Stuidio卻顯示報錯,Intent類顯示為紅色,如圖所示:
Android百度地圖開發學習筆記(二)之定位當前位置和自定義控制元件返回
在完成HelloMap後,接來完成的重要功能是如何定位當前位置和如何一鍵返回。效果圖如下: 這裡的控制元件就是一個ImageView,自己去百度一個好看的圖片就可以了。 一 定位當前位置和自定義控制元件返回 1.官方技術文件 可以先點選百度地圖定位技術文件,仔細看一下相關
Linux開發學習筆記(二)
Shell程式設計 1、簡單介紹 Shell 指令碼(shell script),是一種為 shell 編寫的指令碼程式。Shell 程式設計跟 java、php 程式設計一樣,只要有一個能編寫程式碼的文字編輯器和一個能解釋執行的指令碼直譯器就可以了。簡單地講,shell程式設計就是對一
Android學習筆記(二六) 輸入法
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Android學習筆記(二一) 有趣的widget-日期和時間
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Android學習筆記(二四) 多頁顯示-SlidingDrawer的使用
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
快速開發框架SpringBoot-學習日記(二)
第2章 Spring Boot重要用法 自定義異常頁面 在resources目錄中新建***public/error***目錄 在resources/public/error目錄新建自定義異常頁面,要求檔名稱必須為對應的狀態碼,副檔名為html
嵌入式核心及驅動開發之學習筆記(二) 實現應用控制驅動
Linux系統根據驅動程式實現的模型框架將裝置驅動分成字元裝置驅動、塊裝置驅動、網路裝置驅動三大類。這裡簡單理解一下概念 字元裝置:裝置按位元組流處理資料,通常用的串列埠裝置、鍵盤裝置都是這種。 塊裝置:裝置按塊單位對資料處理,通常是儲存裝置。 網路裝置:顧名思義,建立在soc