I'm developing an application for android 2.3.3 platform and importing Compatibility package for using Fragment class. First of all I wasn't able to run the first trivial example here http://developer.android.com/guide/topics/fundamentals/fragments.html
the application crashes when trying to inflate fragment.
I "solved" hardcoding the content of fragment instead of using xml file - I create an ImageView inside onCreateView:
public static class SampleFragment extends Fragment
{
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ImageView image = new ImageView(getActivity());
image.setImageResource(R.drawable.sample_image);
image.setLayoutParams(new FrameLayout.LayoutParams(120, 30, Gravity.CENTER));
return image;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.and开发者_如何学JAVAroid.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<fragment
android:name="org.blackimp.ListenActivity$SampleFragment"
android:id="@+id/noise_meter_fragment"
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
...
</LinearLayout>
the issue is that it completely ignores the layout width and height set by code - the actual code converts dp in px before calling setLayoutParams. it extends image as to fill the whole fragment width.
- How can I set image width and height - meaning to scale it - ?
- is there a way to make it work by inflating and describing fragment content by xml with compatibility package - yes I extended FragmentActivity but it doesn't work anyway -?
thank you
You have to understand the use of fragments here. You include the fragment in your XML with the width set to "fill_parent". The fragment in this case just returns the ImageView. It is as you would put the ImageView directly into your layout. You have to set the pixel information in the XML of the fragment or you add the fragment programmatically.
You can find more information about scaling ImageViews here.
精彩评论