【发布时间】:2018-03-06 16:07:05
【问题描述】:
我想让我的 ViewPager 具有响应性,但我不知道它为什么不起作用。
我有一个带有 3 个不同选项卡的 TabLayout(目前)使用 ScreenSlide 显示相同片段的 3 次,并且您可以在滑动 viewPager 时切换。
如果我手动设置高度,例如 300dpi 我可以在我的 viewPager 的每个选项卡,但如果我使用 match-parent 或 wrap-content 而不是使用手动高度,我什么都看不到。
也许我的 viewPager 无法获取我的片段或其他东西的尺寸?
感谢您的关注和时间,请原谅我的英语不好。
内容.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardElevation="12dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.anonymous.mh4.monsters.MonsterInfoActivity"
tools:showIn="@layout/activity_monster_info">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/onemonstername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/colorPrimary"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvmonster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ANJANATH"
android:textAlignment="center"
android:textColor="@color/cardview_light_background"
android:textSize="30sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/monstrertypecard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/holo_red_light"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:contentPadding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Wyvern Brutal"
android:textAlignment="center"
android:textColor="@color/cardview_light_background"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:visibility="visible"
app:srcCompat="@drawable/anjaimg" />
<android.support.v7.widget.CardView
android:id="@+id/breakablecard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<TextView
android:id="@+id/breakableview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rompíble: Cabeza, Patas traseras, Cola (cortable)"
android:textAlignment="center"
android:gravity="center"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/gamedescriptioncard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/cardview_shadow_start_color"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/monstericoninfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="false"
card_view:srcCompat="@drawable/bigm1icon" />
<TextView
android:id="@+id/gamedescrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_weight="1"
android:gravity="center"
android:text="Los Anjanath patrullan por el Bosque Primigenio en busca de su comida preferida, los Aptonoth. Este agresivo monstruo ataca a cualquiera sin titubear."
android:textAlignment="center"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<Space
android:layout_width="match_parent"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabItem
android:id="@+id/ecologyTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ecologia" />
<android.support.design.widget.TabItem
android:id="@+id/killTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cómo matarlo" />
<android.support.design.widget.TabItem
android:id="@+id/tipsTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tips" />
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="350dp" />
</LinearLayout>
<android.support.v7.widget.CardView
android:id="@+id/descripcardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/ecology"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="El Anjanath es uno de los depredadores que domina el Bosque Primigenio, donde rivaliza con Rathalos, y también puede encontrarse en el Yermo de Agujas. Se alimenta principalmente de Aptonoth, a los que caza usando su agudo olfato. El Anjanath marca su territorio dejando su saliva pegajosa sobre árboles y rocas para mantener a los intrusos a raya.El Anjanath puede atacar con sus enormes fauces, arremetiendo con su cuerpo o dando coletazos, no obstante, cuando se enfurece es capaz de utilizar fuego para atacar. De forma similar al Glavenus, un órgano en su garganta le permite imbuir su boca en llamas, pudiendo luego expulsar llamaradas para atacar o dar mordiscos."
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/howtokillcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/howtokillview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="El Anjanath es posiblemente el monstruo más peligroso que pulula por el Bosque Primigenio. Es letal a cualquier distancia y sus ataques de Fuego son muy peligrosos. Es muy débil contra el Agua. Tu mejor opción es mantenerte cerca de sus patas traseras (cuidado, cocea, pero es su ataque más débil), golpearle y evitar sus mordiscos y ataques de barrido con la cola. Evita colocarte detrás de él, porque dará latigazos con la cola. Si quieres neutralizar su aliento de Fuego, concentra los ataques en la cabeza hasta romperla."
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/tipscard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tipsview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="Su cabeza y cada pata trasera se pueden romper y su cola se puede cercenar. Atacarle lo suficiente en la cabeza cuando tiene la garganta iluminada puede hacer que caiga. Su oído y su olfato son muy agudos, pero su vista es bastante pobre. En peleas territoriales, vence al Gran Jagras y pierde contra Rathalos."
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
myFragment.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/descripcardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<TextView
android:id="@+id/textViewFragmentEco"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="15dp"
android:text="El Anjanath es El Anjanath es uno de los depredadores que domina el Bosque Primigenio, donde rivaliza con Rathalos, y también puede encontrarse en el Yermo de Agujas. Se alimenta principalmente de Aptonoth, a los que caza usando su agudo olfato. El Anjanath marca su territorio dejando su saliva pegajosa sobre árboles y rocas para mantener a los intrusos a raya.El Anjanath puede atacar con sus enormes fauces, arremetiendo con su cuerpo o dando coletazos, no obstante, cuando se enfurece es capaz de utilizar fuego para atacar. De forma similar al Glavenus, un órgano en su garganta le permite imbuir su boca en llamas, pudiendo luego expulsar llamaradas para atacar o dar mordiscos.uno de los depredadores que domina el Bosque Primigenio, donde rivaliza con Rathalos, y también puede encontrarse en el Yermo de Agujas. Se alimenta principalmente de Aptonoth, a los que caza usando su agudo olfato. El Anjanath marca su territorio dejando su saliva pegajosa sobre árboles y rocas para mantener a los intrusos a raya.El Anjanath puede atacar con sus enormes fauces, arremetiendo con su cuerpo o dando coletazos, no obstante, cuando se enfurece es capaz de utilizar fuego para atacar. De forma similar al Glavenus, un órgano en su garganta le permite imbuir su boca en llamas, pudiendo luego expulsar llamaradas para atacar o dar mordiscos."
android:textAlignment="gravity"
android:textSize="16sp" />
</android.support.v7.widget.CardView>
我尝试了很多不同的 match_parent 和 wrap_content 组合。 (在片段和内容 XML 中)
【问题讨论】:
-
我没有在任何布局文件中看到 ViewPager。
-
第一个 XML,或多或少在中间。 @韦德威尔逊
-
这是一个有趣的问题,我以前从未遇到过。我想知道是否是因为您将 CarView 的高度设置为 wrap_content 而不是显式值。尝试使 CardView 与您声明的 VIewpager 具有相同的高度。如果这不起作用,请尝试将 CardView 替换为 LinearLayout 并查看 Fragment 布局是否填充 ViewPager。
-
"尝试使 CardView 的高度与您声明的 VIewpager 的高度相同。"如果我在片段中设置元素中的固定高度,将在 content.xml 中响应还是仍然是固定高度? @WadeWilson 无论如何,我会在晚上尝试编辑片段 XML,但我不认为这是片段问题。我在这里发布它以尝试专家人员是否可以在 XML 中找到一些解决方案。如果没有人看不到任何东西,我会认为存在代码问题。
-
另外,我对 ConstraintLayout 不熟悉,但由于周围的其他元素,它可能会使您的 ViewPager 变得更小。这完全是猜测,因为我不是 ConstraintLayout 专家。
标签: android android-viewpager responsive