我的活动中有多个动态变化的片段。某些实例片段需要以横向方向添加,而某些实例则以纵向方式添加。如何指定片段必须添加到活动的角度?
我目前的代码:
getfragmentManager.beginTransaction()
.setCustomAnimations(R.animator.fadein, R.animator.fadeout)
.add(R.id.fragment_container, playerPane, tag).commit();
onCreateView方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_player_pane, container, false);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) rootView.getLayoutParams();
// set width height
params.height = (getArguments().getInt(HEIGHT));
params.width = (getArguments().getInt(WIDTH));
params.setMargins((getArguments().getInt(LEFT_MARGIN)),
(getArguments().getInt(TOP_MARGIN)),
(getArguments().getInt(RIGHT_MARGIN)),
(getArguments().getInt(BOTTOM_MARGIN)));
rootView.setLayoutParams(params);
return rootView;
}
答案 0 :(得分:0)
您可以检查设备的配置以查看它是纵向还是横向,并在if语句中使用它来启动不同的片段。
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// launch portrait fragment
} else {
// launch landscape fragment
}
另外,如果我正在编写这个应用程序,我会尽量不根据方向改变添加哪个片段,而是让片段本身通过使用目标XML和检查来根据方向呈现不同的视图在那个片段中。
答案 1 :(得分:0)
井片段将具有一个根视图,您可以为其设置角度并进行旋转。
1. Programatically
view.setRotation((float) 45.0); //example
2. Xml
android:rotation=""
android:rotationX=""
android:rotationY=""
我想知道这样做的必要性。如果它确实需要支持多个方向问题,请通过以下链接