我有以下问题:
我有一个 TabActivity
,显示了 FragmentActivity
在它的标签之一。
I have a TabActivity
that shows a FragmentActivity
in one of its tabs.
这是 FragmentActivity
将 ListFragment
,在该项目点击时 ListFragment
,一个片段添加(也到backstack),并显示出来。
That FragmentActivity
adds a ListFragment
, when clicked on the item of that ListFragment
, a Fragment is added (also to the backstack) and displayed.
现在我需要改变,布局片段
来将横向时改变。
Now I need to change the layout of that Fragment
to change when going to landscape orientation.
不过,我完全一无所知的地方来实现这种变化。我已经创建,纠正在布局陆夹的布局。但如果是正确的点设置呢?
But I'm totally clueless where to implement that change. I have already created to correct layout in the layout-land folder. But where is the correct point to set it?
警告:这可能是pre-棒棒糖答案
Warning: this may be a pre-Lollipop answer.
A 片段
并没有得到重新充气的配置更改,但可以达到的效果如下用一个的FrameLayout 和(重新)填充了手动:
A Fragment
doesn't get re-inflated on configuration change, but you can achieve the effect as follows by creating it with a FrameLayout
and (re)populating that manually:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
FrameLayout frameLayout = new FrameLayout(getActivity());
populateViewForOrientation(inflater, frameLayout);
return frameLayout;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LayoutInflater inflater = LayoutInflater.from(getActivity());
populateViewForOrientation(inflater, (ViewGroup) getView());
}
private void populateViewForOrientation(LayoutInflater inflater, ViewGroup viewGroup) {
viewGroup.removeAllViewsInLayout();
View subview = inflater.inflate(R.layout.my_fragment, viewGroup);
// Find your buttons in subview, set up onclicks, set up callbacks to your parent fragment or activity here.
}
}
我不是特别满意 getActivity()
和相关要求在这里,但我不认为还有另一种方式来抓住那些东西。
I'm not particularly happy with the getActivity()
and related calls here, but I don't think there's another way to get hold of those things.
更新:删除了投的ViewGroup
到的FrameLayout
和使用 LayoutInflater.from()和膨胀()而是明确添加视图。
Update: Removed cast of ViewGroup
to FrameLayout
and used LayoutInflater.from()
, and the third parameter of inflate()
instead of adding the view explicitly.