從0開始的android開發學習筆記,一邊看coursera上的eclipse教學一邊想辦法在android studio上做出來,時間太短要塞的東西太多,怕忘記於是決定筆記起來供未來的自己參考
Create different layout:
landscape: 在res下新增一個名稱為layout-land的android resource directory,將layout資料夾裡的activity_main.xml複製過去就可以另外編輯landscape的layout。一開始選成一般的directory結果完全跑不出來
Internationalization:
讓string支援不同語言: 在strings.xlm上右鍵 -> open translations editor -> add locale (圖示為地球) -> 選擇要新增的語言 ->如果新增的語言沒有出現在translations editor裡面的話就重開translations editor
ScrollView: <ScrollView> 要加scroll的東西 </ScrollView>, android:layout的參數(eg: android:layout_below)要放在scrollView的start tag裡
Export: Build > Generate signed APK
button的onClick函數需是public, return void & 有View的input (ex: public void openWebPage(
View v){} )
<?xml version="1.0" encoding="utf-8"?>
<Buttonxmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
/** Called when the user touches the button */
publicvoid sendMessage(View view){
// Do something in response to button click
}
android:layout_height="0dp" => Means the height of the view can be changed later due to other layout constraints
(mdpi) 160dp = 1 inch; hdpi 240dpi; xhdpi 320dpi; xxhdpi 480dpi; xxxhdpi 640dpi
To prevent configuration changes from recreating the activity, you need to edit the Android manifest file
under "<activity>": android:configChanges="orientation|screenSize|uiMode"
android:screenOrientation="portrait" => 讓app只能portrait view
styles.xml
把常用的style放在style裡方便reuse, 在imageView/TextView裡使用的時候用style = "@style/styleName"
<style name = "LyricsText" parent="@android:style/TextApperance.Large">
- <item name="android:textColor">#404040</item>
- <item name="android:textStyle">italic</item>
</style>
To build an app based on static webpage content, the content should be placed in the "assets" directory (instead of "res"); The correct URL to refer to index.html in the assets directory is "file:///android_asset/index.html"
Code > Optimize Imports => Remove unused imports
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find that webView1
WebView pointer = (WebView) findViewById(R.id.webView1);
// Open asset/index.html
pointer.loadUrl("file:///android_asset/index.html"); }
Add permission: 加<uses-permission> tag inside manifest tag but outside of all other tags
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
pointer.getSettings.getBuiltInZoomControls();
Gets the WebSettings object used to control the settings for this WebView.
Returns
- a WebSettings object that can be used to control this WebView's settings
Without the IntentFilter in the manifest, the activity cannot be called directly
Set a new Intent:
Intent go = new Intent();go.setClass(this, HighScoreScreen.class);startActivity(go); //can use finish() to terminate current activity