使用方向更改保留选项卡样式/布局 [英] preserve tabs style/layout with orientation change

查看:106
本文介绍了使用方向更改保留选项卡样式/布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发我的第一个应用程序,到目前为止一直很好,但是当屏幕方向改变时,标签布局会发生变化。我已经阅读了几个有类似问题的帖子和问题,但找不到好的解释和解决方案。让我来描述我的情况:

在登录活动中输入凭证后,我进入选项卡式活动...

公共类EntryActivity扩展FragmentActivity实现

ActionBar.TabListener

{

私人ViewPager viewPager;

私人TabsPagerAdapter mAdapter;

私人ActionBar actionBar;

//标签标题

private String [] tabs = {Tab1,Tab2,Tab3};

.... 。

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_entry);

//初始化

viewPager =(ViewPager)findViewById(R.id.pager);

actionBar = getActionBar();

actionBar.setDisplayOptions(Window.FEATURE_NO_TITLE);

mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);

actionBar.setHomeButtonEnabled(false);

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

Bundle bundle = getIntent()。getExtras();

username = bundle.getString(username);

//添加标签

for(String tab_name:tabs){

actionBar.addTab(actionBar.newTab()。setText(tab_name)

.setTabListener(this ));

}

....

}

活动条目是用activity_entry定义的。 xml布局

< android.support.v4.view.viewpager>

xmlns:android =http://schemas.android.com/apk/res/android

android:id =@ + id / pager

android:layout_width =match_parent

android:layout_height =match_parent >



每个标签都是用自己的类定义的(示例标签1)

公共类Tab1Fragment扩展片段{

private TextView twUsername;

@Override

public查看onCreateView(LayoutInflater inflater,ViewGroup conta) iner,Bundle savedInstanceState){

查看rootView = inflater.inflate(R.layout.tab_frag1_layout,container,false);

返回rootView;

}

}

和相应的xml文件(tab_frag1_layout,tab_frag2_layout,tab_frag3_layout)



< relativelayout xmlns:android =http://schemas.android.com/apk/res/android>

android: layout_width =match_parent

android:layout_height =match_parent

android:orientation =vertical

android:removed =#fa6a6a >

< textview>

android:id =@ + id / tvFragment1

android:layout_width =fill_parent

android:layout_height =wrap_content

android:gravity =center

android:text =设计最受欢迎的屏幕

android:textSize =20dp

android:layout_centerInParent =true/>



我也定制默认应用程序样式并使用CustomActionBarTheme设置来创建我自己的选项卡

< activity>

android:name =。EntryActivity

和roid:label =@ string / title_activity_entry

android:theme =@ style / CustomActionBarTheme>



当我运行应用程序时在横向方向上一切正常,但是当我更改方向时,选项卡不会保留其自定义样式,而是采用默认样式。我已经阅读了更改方向,你会破坏并重新创建所有活动和片段,但我怎样才能解决我的问题。

提前致谢。





Igor

Hi,
i'm developing my first app, so far so good but stucked with tabs layout change when screen orientation is changed. I've read several posts and questions with similar problem but could not find good explanation and solution. Let me describe my case:
After entering credentials in login activity i enter into tabbed activity...
public class EntryActivity extends FragmentActivity implements
ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3" };
.....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
actionBar.setDisplayOptions(Window.FEATURE_NO_TITLE);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Bundle bundle = getIntent().getExtras();
username = bundle.getString("username");
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
....
}
Activity "entry" is defined with activity_entry.xml layout
<android.support.v4.view.viewpager>
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">

Each Tab is defined with own class (Example Tab1)
public class Tab1Fragment extends Fragment {
private TextView twUsername;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_frag1_layout, container, false);
return rootView;
}
}
and corresponding xml file (tab_frag1_layout, tab_frag2_layout, tab_frag3_layout)

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:removed="#fa6a6a" >
<textview>
android:id="@+id/tvFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Top Rated Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>

I have also customized default application style and used "CustomActionBarTheme" settings to create my own tabs
<activity>
android:name=".EntryActivity"
android:label="@string/title_activity_entry"
android:theme="@style/CustomActionBarTheme" >

When i run application in landscape orientation everything is OK, but when i change orientation, the tabs do not preserve their customized style, but take the default one. I have read that with orientation change you destroy and recreate all activities and fragments but how can i solve my problem.
Thanks in advance.


Igor

推荐答案

这篇关于使用方向更改保留选项卡样式/布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆