为什么我的ImageView不会显示在片段内,而是在活动中显示?

时间:2017-02-28 22:53:54

标签: bitmap imageview

我正在尝试从TabFragment中的drawable设置一个简单的ImageView。这是我创建的代码。通过从可绘制的位图加载,我已经尽可能地简单了。片段中的ImageView永远不会设置为资源位图,但活动中的ImageView会设置。

public class MondayTabFragment extends Fragment
{
  public MondayTabFragment()
  {

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    View view = inflater.inflate(R.layout.fragment_monday_tab, container, false);
    ImageView image = (ImageView) view.findViewById(R.id.myTabView);

    image.setImageResource(R.drawable.valve1);
    return inflater.inflate(R.layout.fragment_monday_tab, container, false);
  }
}

这是关联的XML文件。

<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.terryneckar.sprinkon.MondayTabFragment">    
  <ImageView
    android:layout_width="144dp"
    android:layout_height="144dp"
    app:srcCompat="@android:drawable/alert_light_frame"
    android:id="@+id/myView"
    android:layout_marginStart="80dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

以下是活动内部的代码和相应的XML文件。

public class Schedule extends AppCompatActivity
{ 
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_schedule);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Monday"));
    tabLayout.addTab(tabLayout.newTab().setText("Tuesday"));

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
     {
       @Override
       public void onTabSelected(TabLayout.Tab tab)
       {
         viewPager.setCurrentItem(tab.getPosition());
       }
       @Override
       public void onTabUnselected(TabLayout.Tab tab)
       {
       }
       @Override
       public void onTabReselected(TabLayout.Tab tab)
       {
       }
     });

    View view = this.findViewById(R.id.myTestView);
    ImageView image = (ImageView) view.findViewById(R.id.myTestView);
    image.setImageResource(R.drawable.valve1);
  }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/content_schedule"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  tools:context="com.terryneckar.sprinkon.Schedule"
  tools:showIn="@layout/activity_schedule">

  <android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"/>

  <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_below="@+id/tab_layout" android:layout_alignParentStart="true"/>
  <ImageView
    android:layout_width="194dp"
    android:layout_height="144dp"
    app:srcCompat="@android:drawable/alert_light_frame"
    android:id="@+id/myTestView"
    app:layout_constraintLeft_toLeftOf="parent" android:layout_marginTop="43dp"
    app:layout_constraintTop_toTopOf="parent" android:scaleType="fitXY"
    android:visibility="visible"
    android:layout_below="@+id/tab_layout"
    android:layout_centerHorizontal="true"
    android:contentDescription="test View"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在&#34; onCreateView&#34; &#34; MondayTabFragment&#34;上课,我本应该回来了#34; 查看&#34;而不是&#34; inflater.inflate(R.layout.fragment_monday_tab,container,false)&#34;因为它已经膨胀了。