Android图像未出现在片段中 [英] Android Images not appear in Fragment
问题描述
我制作了一个扩展活动的应用程序,一切正常.为了改善应用程序,我将活动更改为Fragment(使用onCreateView和onActivityCreated),但是现在未显示所有图像.我没有任何错误.
I made an application extending an activity, all worked fine. To improve the application I changed the activity to Fragment (using onCreateView , onActivityCreated), but now all the images not shown. I don't get any errors.
我使用FragmentStatePagerAdapter打开FragmentActivity内部的Fragments.
I open the Fragments inside the FragmentActivity using FragmentStatePagerAdapter.
有什么我想念的吗?
片段XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="10dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="170dp"
android:maxWidth="170dp"
android:minHeight="170dp"
android:maxHeight="170dp"
app:srcCompat="@drawable/logo_pic"
android:id="@+id/iv_profile_img"
android:layout_below="@+id/relativeview_search_bar"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:background="@drawable/circle_image"
/>
</RelativeLayout>
片段类:
public class TempActivity extends Fragment {
private static final String TAG = "SearchCars";
ArrayList<ListUser> userChatItems;
MessagesAdapter mAdapter;
FragmentActivity con;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_listview_users_profiles, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//con = this.getContext();
con = this.getActivity();
Log.v(TAG, "Initializing ...");
View v = getView();
}
}
PageAdapter:
PageAdapter:
public class PageAdapter extends FragmentStatePagerAdapter {
public PageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MyProfileActivity();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 1;
}
}
主要片段活动:
public class MainFragmentActivity extends FragmentActivity{
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);
viewpager = (ViewPager) findViewById(R.id.pager);
PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager());
viewpager.setAdapter(pageAdapter);
}
}
推荐答案
好吧,我发现如果我改为 android:src 而不是 app:srcCompat 一切都很好.
Well I found that if I change to android:src instead of app:srcCompat all is working fine.
从AppCompatActivity扩展时,最好使用app:secCompat但是,当我使用Fragment时,它将不再起作用.
It's probably better to use app:secCompat when extending from AppCompatActivity But, when I use Fragment it doesn't work any more.
这篇关于Android图像未出现在片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!