您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
81_程序的屏幕适配
发布时间:2021-03-25 13:41:40编辑:雪饮阅读()
建立hvga与qvga设备
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我在竖屏下"
/>
<Button
android:id="@+id/hvtab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="横竖屏切换"
android:onClick="doHvtab"
/>
</LinearLayout>
其实这里为了简单起见,两个风格文件中主要只是textview的内容不同。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我在横屏下"
/>
<Button
android:id="@+id/hvtab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="横竖屏切换"
android:onClick="doHvtab"
/>
</LinearLayout>
接下来要实现横竖屏的切换逻辑如MainActivity.java:
package com.example.screenadapt;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doHvtab(View view) {
Configuration mConfiguration = this.getResources().getConfiguration(); //获取设置的配置信息
int ori = mConfiguration.orientation ; //获取屏幕方向
//是横屏
if(ori == mConfiguration.ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
//是竖屏
else if (ori == mConfiguration.ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
}最后布局到这两个设备中,以qvga为横屏,然后对比如下
可见横竖屏独立布局也是成功的实现了。
关键字词:android,屏幕适配
上一篇:80_程序的国际化
下一篇:82_采用代码编写ui