ActionBar - пользовательский вид с центрированным ImageView, элементами действия по сторонам
У меня есть требование центрировать пользовательский логотип (используя ImageView) в панели действий для активности "Главная". Я использую ABS для этого проекта. Это очень похоже на другой вопрос, поставленный на S.O. (логотип ActionBar с центром и элементами действия по сторонам), но я не уверен, что изображение или меню поиска имеет значение, так как я не получаю результатов. Я ищу (центрированное изображение), или если я только что ошибался. В принципе, я устанавливаю значок слева, вставляю пользовательский вид в центре и знамение поиска справа (OptionsMenu). Изображение выглядит немного справа от значка, но оно по-прежнему остается в центре. Любые указатели на то, как центрировать ImageView в панели действий, будут с благодарностью.
Home.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_home, null);
/* Show the custom action bar view and hide the normal Home icon and title */
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_ab_som);
actionBar.setCustomView(customActionBarView);
actionBar.setDisplayShowCustomEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.search, menu);
return true;
}
Рез/макет/actionbar_custom_view_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:id="@+id/actionBarLogo"
android:contentDescription="@string/application_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:duplicateParentState="false"
android:focusable="false"
android:longClickable="false"
android:padding="@dimen/padding_small"
android:src="@drawable/logo_horizontal" />
</LinearLayout>
Рез/меню/search.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@id/search_item"
android:icon="?attr/action_search"
android:title="@string/search_label"
android:showAsAction="ifRoom|collapseActionView">
</item>
</menu>
Ответы
Ответ 1
Разъяснения:
![enter image description here]()
Розовый контейнер - это реальное пространство, в которое вы добавите представление.
Трюк выполняет некоторую математику, чтобы центрировать вид (независимо) до середины.
В моем случае представление было TextView. Здесь мой полный метод:
public void addTextToActionBar( String textToSet )
{
mActionbar.setDisplayShowCustomEnabled( true );
// Inflate the custom view
LayoutInflater inflater = LayoutInflater.from( this );
View header = inflater.inflate( R.layout.actionbar_header, null );
//Here do whatever you need to do with the view (set text if it a textview or whatever)
TextView tv = (TextView) header.findViewById( R.id.program_title );
tv.setText( textToSet );
// Magic happens to center it.
int actionBarWidth = DeviceHelper.getDeviceWidth( this ); //Google for this method. Kinda easy.
tv.measure( 0, 0 );
int tvSize = tv.getMeasuredWidth();
try
{
int leftSpace = 0;
View homeButton = findViewById( android.R.id.home );
final ViewGroup holder = (ViewGroup) homeButton.getParent();
View firstChild = holder.getChildAt( 0 );
View secondChild = holder.getChildAt( 1 );
leftSpace = firstChild.getWidth()+secondChild.getWidth();
}
catch ( Exception ignored )
{}
mActionbar.setCustomView( header );
if ( null != header )
{
ActionBar.LayoutParams params = (ActionBar.LayoutParams) header.getLayoutParams();
if ( null != params )
{
int leftMargin = ( actionBarWidth / 2 - ( leftSpace ) ) - ( tvSize / 2 ) ;
params.leftMargin = 0 >= leftMargin ? 0 : leftMargin;
}
}
}
Разметка:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical|center"
android:orientation="horizontal" >
<TextView
android:id="@+id/program_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:contentDescription="@string/content_description_program_title"
android:ellipsize="end"
android:maxLines="1"
android:textSize="22sp"/>
</RelativeLayout>
Enjoy.
Ответ 2
Если вы хотите просмотреть изображение в Центре ActionBar, используйте:
![enter image description here]()
просто замените getActionBar();
на getSupportActionBar();
ниже кода
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
ваш actionbar_custom_view_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/actionBarLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:longClickable="false"
android:src="@drawable/ic_launcher" />
</LinearLayout>
Скрыть значок панели действий
![enter image description here]()
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
Примечание: для < 11 API использует getSupportActionBar() и > 11 API использует getActionBar()
EDITED: 02/03/16 для панели инструментов
<android.support.v7.widget.Toolbar
style="@style/ToolBarStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material">
<ImageView
android:layout_width="wrap_content"
android:contentDescription="@string/logo"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"/>
</android.support.v7.widget.Toolbar>
Ответ 3
Я столкнулся с этой проблемой, вот мое решение:
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_HORIZONTAL;
actionBar.setCustomView(yourCustomView,layoutParams);
Ответ 4
ImageView в вашем коде центрируется относительно LinearLayout, а не в панели действий. Вы можете добавить левое поле (android: layout_marginLeft) в макет, чтобы отрегулировать положение изображения.
Другой способ сделать это - не добавлять иконку и элементы действия в панель действий, а использовать пользовательский макет с иконкой и кнопками внутри. Но в этом случае вам придется самостоятельно обрабатывать элементы действий.
Ответ 5
Поздняя вечеринка, но в случае, если она помогает кому-либо еще - используйте список слоев и установите его как фон. В противном случае логотип будет центрироваться на основе оставшегося пространства, а не всей панели инструментов, как упоминает Рейнхер.
Вы можете использовать слой-лист со статическим цветом фона, а изображение с гравитацией установить в центр, как показано ниже. Надеюсь, это поможет!
toolbar.axml
<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:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/toolbar_background"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
toolbar_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item>
<bitmap android:src="@drawable/logo" android:gravity="center" />
</item>
</layer-list>
Ответ 6
Я столкнулся с той же проблемой, и предлагаю следующее решение:
-
в вашем res/layout/actionbar_custom_view_home.xml измените ширину макета на wrap_content:
android:layout_width="wrap_content"
-
Получить ширину панели действий следующим образом:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//width of action bar is the same as width of whole screen
final int actionBarWidth = size.x;
-
Добавьте layoutListener в свой customActionBarView:
customActionBarView.addOnLayoutChangeListener(
new OnLayoutChangeListener() {
@Override
public void onGlobalLayout() {
float x = customActionBarView.getX();
int logoImageWidth = imageLogo.getWidth();
int logoPosition = actionBarWidth / 2 - logoImageWidth / 2;
if (x != logoPosition) {
customActionBarView.setX(logoPosition);
customActionBarView.requestLayout();
} else {
customActionBarView.removeOnLayoutChangeListener(this);
}
}
}
);
Ответ 7
Единственное, что я нашел, - это положить (поместить нужное или левое по необходимости, или и то, и другое):
android:layout_marginLeft|Right="?attr/actionBarSize"
который я нашел здесь:
http://sourcey.com/android-custom-centered-actionbar-with-material-design/