Jak wyrównać tytuł na środku paska akcji w domyślnym motywie (motyw.Holo.Światło)
Szukałem dużo, ale nie mogę znaleźć sposobu, jak Mogę ustawić tytuł na środku paska działań zamiast wyrównany do lewej. Użyłem poniższego kodu, aby ustawić tytuł na środku :
ViewGroup decorView= (ViewGroup) this.getWindow().getDecorView();
LinearLayout root= (LinearLayout) decorView.getChildAt(0);
FrameLayout titleContainer= (FrameLayout) root.getChildAt(0);
TextView title= (TextView) titleContainer.getChildAt(0);
title.setGravity(Gravity.CENTER);
Ale daje błąd jak poniżej:
ClassCastException : com.android.internal.widget.ActionBarView can not
be cast to android.widget.TextView.
Każde inne rozwiązanie ..Każda pomoc będzie mile widziana. 11 answers
Można utworzyć układ niestandardowy i zastosować go do paska akcji.
Aby to zrobić, wykonaj te 2 proste kroki:
-
Kod Java
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.actionbar);
Gdzie R.layout.actionbar
jest następującym układem.
-
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="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/action_bar_title" android:text="YOUR ACTIVITY TITLE" android:textColor="#ffffff" android:textSize="24sp" /> </LinearLayout>
To może być tak złożone, jak chcesz. Wypróbuj!
EDIT:
Aby ustawić background
możesz użyć właściwości android:background
w układzie kontenera (LinearLayout w tym case). Może być konieczne ustawienie wysokości układu android:layout_height
na match_parent
zamiast wrap_content
.
Ponadto można również dodać LOGO / ICON do niego. Aby to zrobić, po prostu dodaj ImageView wewnątrz układu i ustaw właściwość orientacji układu android:orientation
na poziomą (lub po prostu użyj RelativeLayout i zarządzaj nim samodzielnie).
Aby dynamicznie zmienić tytuł powyższego niestandardowego paska akcji, wykonaj następujące czynności:
TextView title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName()));
title.setText("Your Text Here");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-26 12:53:07
Wydaje się, że nie ma sposobu, aby to zrobić bez niestandardowego widoku. Możesz uzyskać widok tytułowy:
View decor = getWindow().getDecorView();
TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));
Ale zmiana gravity
lub layout_gravity
nie daje efektu.
Problem w ActionBarView
, który sam układa swoje dzieci tak, że zmiana paramów układu swoich dzieci również nie ma efektu.
Aby zobaczyć ten excecute następujący kod:
ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android"));
View v = actionBar.getChildAt(0);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity= Gravity.CENTER;
v.setLayoutParams(p);
v.setBackgroundColor(Color.BLACK);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-28 14:26:24
Sprawdź nowy pasek narzędzi w klasie biblioteki wsparcia w aktualizacji Lollipop możesz zaprojektować pasek akcji dodając pasek narzędzi w swoim układzie
Dodaj te elementy do motywu aplikacji
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
Stwórz swój pasek narzędzi w układzie i dołącz swój textview w centrum Zaprojektuj swój pasek narzędzi
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/acbarcolor">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Dodaj pasek akcji jako pasek narzędzi
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Upewnij się, że musisz dołączyć pasek narzędzi do pliku zasobów, taki jak ten
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/toolbar" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/homepageinc" />
</FrameLayout>
<fragment
android:id="@+id/fragment1"
android:layout_gravity="start"
android:name="com.shouldeye.homepages.HomeFragment"
android:layout_width="250dp"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-12 10:22:47
Wiem, że moja odpowiedź nie jest na czas, ale jest to czysto kod nr xml
wymagane.
to jest do użytku w Activity
public void setTitle(String title){
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView textView = new TextView(this);
textView.setText(title);
textView.setTextSize(20);
textView.setTypeface(null, Typeface.BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.white));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(textView);
}
to jest do stosowania w Fragment
public void setTitle(String title){
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView textView = new TextView(getActivity());
textView.setText(title);
textView.setTextSize(20);
textView.setTypeface(null, Typeface.BOLD);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.white));
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(textView);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-06 13:32:40
Jeśli używasz paska narzędzi po prostu dodaj
android:layout_gravity="center_horizontal"
Pod paskiem narzędzi tak jak ten fragment
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" //Important
android:textColor="@color/whitecolor"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-23 09:09:26
To faktycznie działa:
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.custom_actionbar);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity = Gravity.CENTER;
Musisz zdefiniować custom_actionbar.układ xml, który jest zgodny z Twoimi wymaganiami np.:
<?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="50dp"
android:background="#2e2e2e"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/top_banner"
android:layout_gravity="center"
/>
</LinearLayout>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-05 06:25:27
Myślę, że pozycjonując widoki na pasku akcji, osiągniesz to, czego chcesz.W układzie paska akcji gdy params układu jest ustawiony na pasujący do rodzica, nie pasuje do pełnej szerokości dlatego zrobiłem to programowo, gdzie succeed.By ustawiając szerokość (działa jak layout_weight= "value") możemy ustawić nasz Widok gdzie chcemy:
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater ll = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) ll.inflate(R.layout.custom_actionbar, null);
//inner layout within above linear layout
LinearLayout inner = (LinearLayout) linearLayout.findViewById(R.id.inner_linear_ractionbar);
inner.setMinimumWidth(getWidth() * 2 / 10);
// we will set our textview to center and display there some text
TextView t = (TextView) linearLayout.findViewById(R.id.center_text);
t.setWidth(getWidth() * 6 / 10);
Button b = (Button) linearLayout.findViewById(R.id.edit_button);
b.setWidth(getWidth() *3/ 20);
ImageButton imageButton = (ImageButton) linearLayout.findViewById(R.id.action_bar_delete_item);
imageButton.setMinimumWidth(deviceUtils.getWidth() / 20);
A oto metoda getWidth ():
public int getWidth() {
DisplayMetrics dm = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-24 06:03:45
Kod Java:
napisz to w onCreate()getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.action_bar);
A dla własnego widoku, po prostu użyj FrameLayout, east peasy!
android.wsparcie.v7.widget.Pasek narzędzi jest inną opcją
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/app_name"
android:textColor="@color/black"
android:id="@+id/textView" />
</FrameLayout>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-24 09:19:39
Miałem ten sam problem, a ponieważ przycisk "Home" dodany automatycznie na pasku narzędzi, mój tekst nie został dokładnie wprowadzony.
Naprawiłem to w brudny sposób, ale działa dobrze w moim przypadku. Po prostu dodałem margines po prawej stronie widoku tekstu, aby zrekompensować przycisk home po lewej stronie. Oto mój układ paska narzędzi:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="1dp"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:gravity="center"
android:background="@color/mainBackgroundColor"
android:fitsSystemWindows="true" >
<com.lunabee.common.utils.LunabeeShadowTextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="?attr/actionBarSize"
android:gravity="center"
style="@style/navigation.toolbar.title" />
</android.support.v7.widget.Toolbar>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-10 16:02:11
Możesz wymusić pasek narzędzi, owijając tytuł i poziom w prawo padding, który ma domyślne lewe padding dla tytułu. Następnie umieść kolor tła do rodzica paska narzędzi i w ten sposób część, która jest wycięta przez owijanie tytułu, jest w tym samym kolorze (biały w moim przykładzie):
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_gravity="center_horizontal"
android:paddingEnd="15dp"
android:paddingRight="15dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:titleTextColor="@color/black"/>
</android.support.design.widget.AppBarLayout>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-03 19:59:41
Rozwiązanie opiera się na tych rzeczach:
- musisz użyć własnej klasy, która rozszerza pasek narzędzi (support.v7.widget.toolbar)
- musisz nadpisać jeden pasek narzędzi metody # addView
Co to robi:
Gdy pasek narzędzi po raz pierwszy wykonuje # setTitle, tworzy AppCompatTextView i używa go do wyświetlania tekstu tytułu.
Po utworzeniu AppCompatTextView, toolbar (jako ViewGroup) dodaje go do własnej hierarchii za pomocą #addView metoda.
Również, próbując znaleźć rozwiązanie zauważyłem, że textview ma szerokość układu ustawioną na "wrap_content", więc postanowiłem zrobić go "match_parent" i przypisać textalignment do "center".
MyToolbar.kt, pomijając niezwiązane sprawy (konstruktorzy/importerzy):class MyToolbar : Toolbar {
override fun addView(child: View, params: ViewGroup.LayoutParams) {
if (child is TextView) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT
child.textAlignment= View.TEXT_ALIGNMENT_CENTER
}
super.addView(child, params)
}
}
Możliwe "skutki uboczne" - dotyczy to również "napisów"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-06 16:07:31