我的应用程序有一个 Gridview。单击用于打开在清单中以对话框为主题的活动的 Gridview 单元格。这运作良好。
为了进一步改进应用程序,我想在活动中添加一个查看器。所以我SomeClass extends Activity
改为SomeClass extends Fragment
. 我创建了一个容器(其中extends FragmentActivity
)来保存它Fragment
,并将这个容器主题为清单中的对话框:
<activity android:name="com.packagename.SomeContainer" android:theme="@style/Theme.Sherlock.Light.Dialog" />
这不会将片段显示为对话框。它显示了我不想要的片段全屏,并且它不会根据内容调整大小,当它是一个活动时它会这样做。
我应该如何将容器(fragmentactivity)或片段显示为应根据内容调整大小而不是全屏显示的对话框?
SomeContainer.java
public class SomeContainer extends SherlockFragmentActivity {
private SomeAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
overridePendingTransition(R.anim.pull_in_from_left, R.anim.hold);
setContentView(R.layout.layout_events);
getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Intent intent = getIntent();
String date_string = intent.getStringExtra("date_string");
mAdapter = new SomeAdapter(getSupportFragmentManager(), date_string);
mPager = (ViewPager) findViewById(R.id.eventspager);
mPager.setAdapter(mAdapter);
}
@Override
protected void onPause() {
overridePendingTransition(R.anim.hold, R.anim.pull_out_to_left);
super.onPause();
}
public static class SomeAdapter extends FragmentPagerAdapter {
String date;
FragmentManager fm;
public SomeAdapter(FragmentManager fm, String date) {
super(fm);
this.date = date;
this.fm = fm;
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SomeFragment.newInstance(date);
case 1:
return SomeFragment1.newInstance(date);
case 2:
return SomeFragment2.newInstance(date);
default:
return null;
}
}
}
}
SomeFragment.java
public class SomeFragment extends DialogFragment {
String date_string;
public static SomeFragment newInstance(String date) {
SomeFragment someFragment = new SomeFragment();
Bundle args = new Bundle();
args.putString("date", date);
someFragment.setArguments(args);
returnsomeFragment;
}
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.events, container, false);
//getActivity().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
date_string = getArguments().getString("date");
//do something
return v;
}
}