Created
June 18, 2018 14:30
-
-
Save hackenbacker/b10aca699a9c9042d76f3b2ced098d65 to your computer and use it in GitHub Desktop.
FragmentTabHost Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.app.FragmentTabHost | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@android:id/tabhost" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity" | |
android:animateLayoutChanges="true"> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_above="@android:id/tabs" | |
android:orientation="vertical"> | |
<FrameLayout | |
android:id="@android:id/tabcontent" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:visibility="gone"/> | |
<FrameLayout | |
android:id="@+id/content" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> | |
<TabWidget | |
android:id="@android:id/tabs" | |
android:layout_alignParentBottom="true" | |
android:layout_width="match_parent" | |
android:layout_height="50dp" | |
android:background="#dddddd" | |
android:orientation="horizontal" /> | |
</RelativeLayout> | |
</android.support.v4.app.FragmentTabHost> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
FragmentManager fragmentManager = getSupportFragmentManager(); | |
FragmentTabHost host = findViewById(android.R.id.tabhost); | |
host.setup(this, fragmentManager, R.id.content); | |
TabHost.TabSpec tabSpec1 = host.newTabSpec("tab1"); | |
Button button1 = new Button(this); | |
button1.setText("甲"); | |
button1.setBackgroundColor(0x00000000); | |
tabSpec1.setIndicator(button1); | |
Bundle bundle1 = new Bundle(); | |
bundle1.putString("name", "甲"); | |
host.addTab(tabSpec1, SampleFragment.class, bundle1); | |
TabHost.TabSpec tabSpec2 = host.newTabSpec("tab2"); | |
Button button2 = new Button(this); | |
button2.setText("乙"); | |
button2.setBackgroundColor(0x00000000); | |
tabSpec2.setIndicator(button2); | |
Bundle bundle2 = new Bundle(); | |
bundle2.putString("name", "乙"); | |
host.addTab(tabSpec2, SampleFragment.class, bundle2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_marginBottom="20dp" | |
android:layout_above="@id/text_view"> | |
<Button | |
android:id="@+id/show_button" | |
android:layout_width="160dp" | |
android:layout_height="50dp" | |
android:layout_margin="8dp" | |
android:text="Show" /> | |
<Button | |
android:id="@+id/hide_button" | |
android:layout_width="160dp" | |
android:layout_height="50dp" | |
android:layout_margin="8dp" | |
android:text="Hide" /> | |
</LinearLayout> | |
<TextView | |
android:id="@+id/text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="36dp" | |
android:textColor="#000000" | |
tools:text="甲" | |
android:layout_centerInParent="true"/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SampleFragment extends Fragment implements OnClickListener { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.sample_fragment, null); | |
TextView textView = view.findViewById(R.id.text_view); | |
String name = getArguments().getString("name"); | |
textView.setText(name); | |
Button hideButton = view.findViewById(R.id.hide_button); | |
Button showButton = view.findViewById(R.id.show_button); | |
hideButton.setOnClickListener(this); | |
showButton.setOnClickListener(this); | |
return view; | |
} | |
public void onClick(View view) { | |
switch (view.getId()) { | |
case R.id.hide_button: | |
hideActionBar(); | |
break; | |
case R.id.show_button: | |
showActionBar(); | |
break; | |
default: | |
break; | |
} | |
} | |
private void showActionBar() { | |
ActionBar actionBar = getActionBar(); | |
if (actionBar != null) { | |
actionBar.show(); | |
} | |
} | |
private void hideActionBar() { | |
ActionBar actionBar = getActionBar(); | |
if (actionBar != null) { | |
actionBar.hide(); | |
} | |
} | |
private ActionBar getActionBar() { | |
return ((AppCompatActivity)getActivity()).getSupportActionBar(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment