【发布时间】:2011-10-12 11:31:12
【问题描述】:
我正在为 android 2.3.3 平台开发一个应用程序并导入 Compatibility 包以使用 Fragment 类。 首先我无法在这里运行第一个简单的例子http://developer.android.com/guide/topics/fundamentals/fragments.html
应用程序在尝试膨胀片段时崩溃。
我“解决”了对片段内容进行硬编码而不是使用 xml 文件 - 我在 onCreateView 中创建了一个 ImageView:
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.android.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>
问题在于它完全忽略了代码设置的布局宽度和高度 - 实际代码在调用 setLayoutParams 之前将 dp 转换为 px。它扩展图像以填充整个片段宽度。
- 如何设置图像的宽度和高度 - 意思是缩放它 - ?
- 有没有办法通过使用兼容性包的 xml 膨胀和描述片段内容来使其工作 - 是的,我扩展了 FragmentActivity 但它无论如何都不起作用 - ?
谢谢
【问题讨论】: