Android Tabhostについて
- 評価
- クリップ 0
- VIEW 1,968


import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
ViewPager viewPager;
TabHost tabHost;
View indicator;
TabWidget tabWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//マテリアルデザインの作業
tabWidget = (TabWidget) findViewById(android.R.id.tabs);
tabWidget.setStripEnabled(false);
tabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
indicator = findViewById(R.id.indicator);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
getActionBar().setElevation(0); //TabとActionBarとの間の影を消す
float elevation = 4 * getResources().getDisplayMetrics().density;
tabHost.setElevation(elevation);
//PagerとTabの設定
ViewPagerAdapter vpa = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(vpa);
viewPager.setOnPageChangeListener(new PageChangeListener());
for (int i = 0; i < vpa.getCount(); i++) {
tabHost.addTab(tabHost
.newTabSpec(String.valueOf(i))
.setIndicator(vpa.getPageTitle(i))
.setContent(android.R.id.tabcontent));
}
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < vpa.getCount(); i++) {
TextView tv = (TextView) inflater.inflate(R.layout.tab_widget, tabWidget, false);
tv.setText(vpa.getPageTitle(i));
tabHost.addTab(tabHost
.newTabSpec(String.valueOf(i))
.setIndicator(tv)
.setContent(android.R.id.tabcontent));
}
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
viewPager.setCurrentItem(Integer.valueOf(tabId));
}
});
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
tabHost.setCurrentTab(position);
}
});
}
private class PageChangeListener implements ViewPager.OnPageChangeListener {
private int scrollingState = ViewPager.SCROLL_STATE_IDLE;
@Override
public void onPageSelected(int position) {
// スクロール中はonPageScrolled()で描画するのでここではしない
if (scrollingState == ViewPager.SCROLL_STATE_IDLE) {
updateIndicatorPosition(position, 0);
}
tabWidget.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
scrollingState = state;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
updateIndicatorPosition(position, positionOffset);
}
private void updateIndicatorPosition(int position, float positionOffset) {
View tabView = tabWidget.getChildTabViewAt(position);
int indicatorWidth = tabView.getWidth();
int indicatorLeft = (int) ((position + positionOffset) * indicatorWidth);
final FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) indicator.getLayoutParams();
layoutParams.width = indicatorWidth;
layoutParams.setMargins(indicatorLeft, 0, 0, 0);
indicator.setLayoutParams(layoutParams);
}
}
}
このコードで、下の写真のようなtabを作りたいのですが、上のようになってしまします。どうしてでしょう?
レイアウトはこちらです。
res/layout/activity_main.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"
android:orientation="vertical"
tools:context=".MainActivity">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/colorPrimary">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" />
<View
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="bottom"
android:background="?android:attr/colorControlActivated" />
</TabHost>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
res/layout/left_view_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Left Fragment" />
</LinearLayout>
res/layout/center_view_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Center Fragment" />
</LinearLayout>
res/layout/right_view_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Right Fragment" />
</LinearLayout>
res/layout/tab_widget
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="bottom|center_horizontal"
android:paddingBottom="16dp"
android:textColor="@color/tab_widget_text"
android:textSize="14sp"
tools:layout_width="match_parent"
tools:text="group 1 ITEM TWO" />
res/color/tab_widget_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFFFF" android:state_selected="true" />
<item android:color="#99FFFFFF" />
</selector>
</selector>
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
まだ回答がついていません
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.09%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正の依頼
RitsukiGoto
2015/07/11 21:27
レイアウトのソースを貼っていただけますか?
TakamasaAwai
2015/07/11 22:00
ありがとうございます。少々お待ちください。
TakamasaAwai
2015/07/11 22:10
完了しました。他のものは多く追加しただけです。気にしないでください。