[英]Android Toolbar
我對如何在應用程序中從ActionBar
更改為toolbar
感到非常困惑。 我看到了很多文檔,但找不到針對我的問題的任何解決方案。 歡迎提供有關我的代碼的任何幫助。 我需要為新工具欄更改所有操作actionbar
(不建議使用),並且無法在已有的類中設置工具欄。 這個問題的線索在於我的擴展或實現。 我不知道,謝謝你的建議!
public class Quizz_View extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private DrawerLayout leftDrawer;
private ListView drawerListView;
private ActionBar ldActionBar;
private Toolbar toolBar;
private ActionBarDrawerToggle abDrawerToggle;
// private ArrayList<Class_Questions> questionsList = new
// ArrayList<Class_Questions>();
GridView gridView;
float xInicial;
float yInicial;
float xActual;
float yActual;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
//menu del toolbar
//toolbar.inflateMenu(R.menu.MENU);
//toolbar.setTitle("AD NORMA");
//toolbar.setSubtitle("INGENIAT");
Button reply = (Button) findViewById(R.id.replyBtn);
reply.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
final Button finish = (Button) findViewById(R.id.finishBtn);
finish.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
finish.setVisibility(View.INVISIBLE);
/********************/
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// crea_menu_navegacion();
// Set the adapter for the list view
// drawerListView.setAdapter();
leftDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
abDrawerToggle = new ActionBarDrawerToggle(this, leftDrawer, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
leftDrawer.setDrawerListener(abDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
leftDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// drawerListView.setOnItemClickListener(new DrawerItemClickListener());
/***********************/
// Inicialización
ArrayList<Integer> inte = new ArrayList<Integer>();
viewPager = (ViewPager) findViewById(R.id.pager);
// ArrayList<Integer> pageNumber = new ArrayList<Integer>();
actionBar = getActionBar();
try {
// ENTRA A PAGINAS
JSONObject jsonQuiz = new JSONObject(stringQuiz);
jsonPaginas = jsonQuiz.getString("Paginas");
JSONArray jsn = new JSONArray(jsonPaginas);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), jsonPaginas);
for (Integer i = 0; i < jsn.length(); i++) {
actionBar.addTab(actionBar.newTab().setText("Página " + (i + 1)).setTabListener(this));
}
} catch (Exception e) {
}
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (position >= 5)
finish.setVisibility(View.VISIBLE);
else
finish.setVisibility(View.INVISIBLE);
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private ActionBar getSupportActionBar() {
return null;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
和XML
<android.support.v4.widget.DrawerLayout /android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
<include
android:id="@+id/toolbarr"
layout="@layout/toolbarex"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="28sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="bottom"
android:textSize="28sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="28sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:textSize="28sp" />
<Button
android:id="@+id/replyBtn"
style="@style/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:layout_marginLeft="20dp"
android:text="Responder" />
<Button
android:id="@+id/finishBtn"
style="@style/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="18dp"
android:layout_marginRight="26dp"
android:text="Terminar" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/darker_gray"
android:choiceMode="singleChoice"
android:divider="@android:color/background_dark"
android:dividerHeight="1dp" />
在布局中添加工具欄:
<android.support.v7.widget.Toolbar
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”@dimen/triple_height_toolbar”
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
在styles.xml中更改以下內容以刪除操作欄
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
</style>
在MainActivity.java中
Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);
setSupportActionBar(toolbar);
toolbar.setTitle("ToolBar Demo");
toolbar.setLogo(R.drawable.ic_launcher);
將工具欄Xml代碼放在Viewpager之前。 ViewPager可能占用了整個屏幕的高度,而工具欄卻在后面。
只需將以下代碼放在viewpager之前,而不是之后。
<include
android:id="@+id/toolbarr" layout="@layout/toolbarex"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
一項澄清:在新版本的支持庫中,您應該使用AppCompatActivity
步驟1:使用NoActionBar父主題。
<!-- Base application theme. --> <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
步驟2:從AppCompatActivity派生您的活動。
public class MainActivity extends AppCompatActivity
步驟3:將工具欄添加到層次結構中。
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
步驟4:將工具欄附加到您的AppCompatActivity。
在MainActivity中: Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
現在,您可以根據需要使用getSupportActionBar()來像ActionBar一樣使用工具欄。
這是它的代碼。
Build.Gradle文件
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
創建一個新的布局文件toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@color/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
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="wrap_content"
android:orientation="vertical"
tools:context="com.ahotbrew.toolbar.MainActivity">
<include layout="@layout/toolbar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" >
<TextView
android:text="@string/hello_brew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
這是創建工具欄的方式。 該示例來自http://www.ahotbrew.com/android-toolbar-example/
如果您需要在底部顯示工具欄,則可以在鏈接中找到代碼片段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.