這篇文章主要介紹了Android Studio屏幕方向以及UI界面狀態(tài)的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)長(zhǎng)期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為思明企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站,思明網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
項(xiàng)目:Orientation
package com.example.orientation; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 1.禁止切換橫屏:在 AndroidManifest.xml-->application->activity->中設(shè)置如下代碼(android:screenOrientation="portrait")2. 創(chuàng)建 Landscape 布局,橫屏?xí)r,會(huì)自動(dòng)加載 Landscape 的布局界面(清單文件中,注意去掉 android:screenOrientation="portrait" ) 3. 翻轉(zhuǎn)屏幕時(shí),保存窗口控件的狀態(tài)值; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ Button button; TextView textView; String TAG = "myTag"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button ); textView = findViewById(R.id.textView); //如果State中的值不為空,如果有相應(yīng)的這個(gè)組件的值,則讀取出來(lái)賦值上去 if(savedInstanceState !=null) { String s = savedInstanceState.getString("key"); textView.setText(s); } button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView.setText(button.getText()); } }); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy:"); } @Override //將 textView 中的值,先保存到 outState 中(鍵值對(duì)) public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("key",textView.getText().toString()); } }
擴(kuò)展學(xué)習(xí):
UI界面設(shè)計(jì)
TextView
要想使得文字居中,需要添加屬性android:gravity="center",可選擇的選項(xiàng)還有top、bottom、left、right、center等,center相當(dāng)于center_vertical|center_horizontal。
使用android:textSize="24sp"指定文字大小,android:textColor="#00ff00"指定文字顏色。
Button
在Android中,Button上面的文字默認(rèn)英文全部大寫,可以通過(guò)設(shè)置android:textAllCaps="false"改變
EditText
通過(guò)設(shè)置hint屬性可以得到提示文字,設(shè)置maxLines使得輸入框中最大輸入行數(shù)。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android Studio屏幕方向以及UI界面狀態(tài)的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!