我有一个承载 4 个片段的视图寻呼机:这是 viewPagerActivity 的 xml
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" />
<android.support.v4.view.ViewPager
android:id="@+id/tabviewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</TabHost>
现在在其中一个片段中,我有一个 Listview。在其 onListItemClick 函数中,我编写了上面的代码,以便每次单击列表上的项目时,都会出现一个片段,提供一些额外的信息。我已经完成了演示(我只有一个 LinearLayout,我会在那里添加它)。但我不能在我的应用程序中这样做。
public void onListItemClick(ListView l, View v, int position, long id) {
int MARGIN = 16;
ft = getActivity().getSupportFragmentManager().beginTransaction();
RightFragment rightFragment = new RightFragment(getResources().getColor(
R.color.honeycombish_blue), 2f, MARGIN / 2, MARGIN, MARGIN,
MARGIN);
ft.add(R.id.,rightFragment );
ft.commit();
}
那是我的片段类:
public class RightFragment extends SherlockFragment {
private View mView;
private int mColour;
private float mWeight;
private int marginLeft, marginRight, marginTop, marginBottom;
// need a public empty constructor for framework to instantiate
public RightFragment() {
}
public RightFragment(int colour, float weight, int margin_left,
int margin_right, int margin_top, int margin_bottom) {
mColour = colour;
mWeight = weight;
marginLeft = margin_left;
marginRight = margin_right;
marginTop = margin_top;
marginBottom = margin_bottom;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mView = new View(getActivity());
GradientDrawable background = (GradientDrawable) getResources()
.getDrawable(R.drawable.rounded_rect);
background.setColor(mColour);
mView.setBackgroundDrawable(background);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, mWeight);
lp.setMargins(marginLeft, marginTop, marginRight, marginBottom);
mView.setLayoutParams(lp);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("'");
mView = inflater.inflate(R.layout.myfrg, container, false);
GradientDrawable background = (GradientDrawable) getResources()
.getDrawable(R.drawable.rounded_rect);
background.setColor(mColour);
mView.setBackgroundDrawable(background);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, mWeight);
lp.setMargins(marginLeft, marginTop, marginRight, marginBottom);
mView.setLayoutParams(lp);
return mView;
}
}
!我应该使用哪个布局作为我的新片段的根。希望图片有帮助1