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



我正在尝试从tabragment内的可绘制的图像视图设置一个简单的图像视图。这是我创建的代码。我通过从可绘制的位图中加载来使其尽可能简单。片段中的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>

在"星期一tabfragment"类的" oncreateview"中,我应该返回" view ",而不是" forflater.inflate(r.layout)。fragment_monday_tab,容器,false)"因为它已经被夸大了。

最新更新