我的活动首先显示一个WebView片段,它必须有0填充,以便WebView触及屏幕的边缘。然后它转换为ScrollView片段,ScrollView内部必须有12dp填充(而不是Activity),以便滚动条位于边缘但内容被填充。但是,如果ScrollView片段是双窗格活动布局的一部分,则只需填充左侧。
答案 0 :(得分:0)
@Override
public void onStart() {
super.onStart();
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//modify the padding
}
}
修改强>
我们假设我们有第一个显示项目列表的片段,第二个片段显示详细信息,使用此代码,如果存在,您将重用DetailsFragment实例。
public boolean multiPane() {
return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(multiPane()) {
DetailsFragment details = (DetailsFragment)getSupportFragmentManager().findFragmentByTag("DETAILS_FRAGMENT");
if (details != null) {
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().beginTransaction().remove(details).commit();
getSupportFragmentManager().executePendingTransactions();
getSupportFragmentManager().beginTransaction().replace(R.id.multi_details, details, "DETAILS_FRAGMENT").commit();
} else {
//create and show a default details fragment(e.g. the first itme on the list idk)
}
} else {
MyListFragment lf = new MyListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.list_and_details, lf, "LIST_FRAGMENT").commit();
}
}
这部分代码用于在将DetailsFragment从一个FrameLayout容器(纵向xml)更改为另一个容器(横向xml)时避免异常。
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().beginTransaction().remove(details).commit();
getSupportFragmentManager().executePendingTransactions();
例外是:
...java.lang.IllegalStateException: Can't change container ID of fragment DetailsFragment...
这是一个非常基本的例子
答案 1 :(得分:0)
我通过从Activity发送填充作为参数来解决它。如果存在,则参数将覆盖Fragment的默认填充。
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_register, container, false);
if (this.getArguments() != null) {
view.setPadding(this.getArguments()
.getInt(ARG_PADDING_LEFT, view.getPaddingLeft()), view.getPaddingTop(), this.getArguments()
.getInt(ARG_PADDING_RIGHT, view.getPaddingRight()), view.getPaddingBottom());
}
return view;
}