android 方向控制界面,Android Studio屏幕方向以及UI界面状态的保存代码详解
項(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 {
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
本實(shí)例主要學(xué)習(xí),屏幕翻轉(zhuǎn)時(shí),界面如何自適應(yīng),創(chuàng)建橫屏布局
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è)組件的值,則讀取出來賦值上去
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:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="this is a textview"
android:textcolor="#00ff00"
android:textsize="24sp" />
要想使得文字居中,需要添加屬性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:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button"
android:textallcaps="false"/>
在android中,button上面的文字默認(rèn)英文全部大寫,可以通過設(shè)置android:textallcaps="false"改變
edittext
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="helloworld"
android:maxlength="20"
android:maxlines="1" />
通過設(shè)置hint屬性可以得到提示文字,設(shè)置maxlines使得輸入框中最大輸入行數(shù)。
以上相關(guān)知識(shí)點(diǎn)如果還有什么疏漏大家可以直接聯(lián)系小編,感謝你的閱讀和對(duì)萬仟網(wǎng)的支持。
總結(jié)
以上是生活随笔為你收集整理的android 方向控制界面,Android Studio屏幕方向以及UI界面状态的保存代码详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 上下扫描动画,Andro
- 下一篇: android 不可点击状态,Andro