質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

643閲覧

Tablayoutをnestした際、tabがツールバーにかぶってしまい、うまく表示されない

akawo

総合スコア23

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2018/09/02 14:17

【問題】
Tablayoutをnestした際、nestされた側のtabが表示されない

【現状】
上下にtabがあるレイアウトを実現したく、tablayoutとviewpagerを使用しております。
その際、nestされた側のtab(画面の上側)がツールバーと被っており、うまく表示されません。

ググってみたのですが、なかなかそのような現象を記載しているものはなかったので、ここで質問させていただいております、、、

【コード】

activity_main.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:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tabTab01" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:tabGravity="fill" app:tabIndicatorColor="@android:color/holo_blue_light" app:tabMaxWidth="0dp" app:tabMode="fixed" app:tabTextAppearance="@style/TextAppearance.AppCompat.Medium" /> <android.support.v4.view.ViewPager android:id="@+id/styleviewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" app:layout_constraintBottom_toTopOf="@+id/tabTab01" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> </android.support.constraint.ConstraintLayout>

Mainactivity.kt

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //ViewPagerに先ほど作成したAdapterのインスタンスを渡してあげる styleviewpager.adapter = FGadapter(supportFragmentManager, this) //TabLayoutにViewPagerのインスタンスを渡すと自動的に実装してくれる tabTab01.setupWithViewPager(styleviewpager) //Tabへの処理はsetupWithViewPagerをした後だったら可能 //ここではタブ名とアイコンを設定している val tab1 = tabTab01.getTabAt(0) tab1?.text = "追加" val tab2 = tabTab01.getTabAt(1) tab2?.text = "一覧" } }

basefragment1.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" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TabLayout android:id="@+id/childTab" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:tabGravity="fill" app:tabIndicatorColor="@android:color/holo_blue_light" app:tabMaxWidth="0dp" app:tabMode="fixed" /> <android.support.v4.view.ViewPager android:id="@+id/childviewpager" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/childTab" /> </android.support.constraint.ConstraintLayout>

FGadapter.kt

class FGadapter(fm: FragmentManager, context: Context) : FragmentPagerAdapter(fm) { override fun getItem(position: Int): Fragment { return when (position) { 0 -> { basefragment1() } 1 -> { basefragment2() } else -> { basefragment1() } } } override fun getCount(): Int { return 2 } }

basefragment1.kt

class basefragment1: Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.basefragment1, null) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) childviewpager.adapter = CHFGadapter(childFragmentManager, this) childTab.setupWithViewPager(childviewpager) val childtab1 = childTab.getTabAt(0) childtab1?.text = "A" val childtab2 = childTab.getTabAt(1) childtab2?.text = "B" } }

暫定的に上側からいくらか距離を指定することで、ツールバーとtabが被るのは防ぐことはできるのですが、
非常に気持ち悪い状態で、かつなかなか解決できておりません。

よろしければ、アドバイスいただければ幸いです。
何卒よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自分の不注意でした、、、

android:layout_height="wrap_content"

android:layout_height="match_constraint"

で解決しました!
ありがとうございました!!

投稿2018/09/03 12:53

akawo

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問