这篇文章主要介绍了Android Studio屏幕方向以及UI界面状态的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
项目: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->中设置如下代码(android:screenOrientation="portrait")
<activity android:name=".MainActivity" android:screenOrientation="portrait" >
2. 创建 Landscape 布局,横屏时,会自动加载 Landscape 的布局界面(清单文件中,注意去掉 android:screenOrientation="portrait" )
3. 翻转屏幕时,保存窗口控件的状态值;
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
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中的值不为空,如果有相应的这个组件的值,则读取出来赋值上去
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 中(键值对)
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("key",textView.getText().toString());
}
}
扩展学习:
UI界面设计
TextView
<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",可选择的选项还有top、bottom、left、right、center等,center相当于center_vertical|center_horizontal。
使用android:textSize="24sp"指定文字大小,android:textColor="#00ff00"指定文字颜色。
Button
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false"/>
在Android中,Button上面的文字默认英文全部大写,可以通过设置android:textAllCaps="false"改变
EditText
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HelloWorld"
android:maxLength="20"
android:maxLines="1" />
通过设置hint属性可以得到提示文字,设置maxLines使得输入框中最大输入行数。
感谢你能够认真阅读完这篇文章,希望小编分享的“Android Studio屏幕方向以及UI界面状态的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。