簡體   English   中英

片段無法顯示圖片

[英]Fragment can't show image

我對在Android中使用Fragments很陌生,並嘗試使用View Pager為我的應用程序構建一個簡單的圖像滑塊。 我已經有了代碼,並且無法正常工作,應用程序運行時未顯示圖像,任何人都可以幫助我解決此問題。 這是我的代碼

fragment_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wk.sigah.FragmentHome"
android:background="@color/colorPrimary">

<!-- TODO: Update blank fragment layout -->
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:id="@+id/viewImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>
</ScrollView>

FragmentHome.java

public class FragmentHome extends Fragment {
ViewPager viewPager;
customSwitchAdapterHome adapter;
public FragmentHome() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    /**return inflater.inflate(R.layout.fragment_fragment_home, container, false);*/
    View view=inflater.inflate(R.layout.fragment_fragment_home,container,false);
    viewPager=(ViewPager)view.findViewById(R.id.viewImage);
    adapter=new customSwitchAdapterHome(this.getActivity());
    viewPager.setAdapter(adapter);
    return view;
}

customSwitchAdapterHome.java

public class customSwitchAdapterHome extends PagerAdapter {
private int[] image_resource={R.drawable.background_bottom_nav,R.drawable.background_login_two};
private Context ctx;
private LayoutInflater layoutInflater;

public customSwitchAdapterHome(Context ctx){
    this.ctx=ctx;
}

@Override
public int getCount() {
    return image_resource.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return(view==(LinearLayout)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view=layoutInflater.inflate(R.layout.swipe_layout,container,false);
    ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
    TextView textView=(TextView)item_view.findViewById(R.id.image_count);
    imageView.setImageResource(image_resource[position]);
    textView.setText(" "+position);
    container.addView(item_view);
    return item_view;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
   container.removeView((LinearLayout)object);
}

swipe_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/image_count"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:text="Hello World" />
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"/>
</LinearLayout>

這是因為LinearLayout中垂直方向的TextView的高度為match_parent ,imageView無法容納。因此,要解決此問題,可以使用wrap_content來textView或,

  android:layout_weight="1"

和使用

  android:layout_height="0dp"

線性布局中的textView和imageView

您的布局混亂了。 請嘗試以下布局。 TextView高度設置為wrap_content 如果將height設置為match_parent則它將覆蓋整個LinearLayout因此不會顯示ImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
            <TextView
                android:id="@+id/image_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginBottom="20dp"
                android:text="Hello World" />
            <ImageView
                android:id="@+id/image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </LinearLayout>

這只是一個示例,原因是我不知道ui的確切含義。布局也是基本的構建塊,因此您應該開始閱讀User Interface&Navigation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM