2015-01-16 2 views
1

안녕하세요 여러분 모두 맞춤 작업 모음을 만들려고합니다. 아래 내 코드. 모든 것은 Action Bar의 오른쪽에 좋지만 왼쪽의 Custom Action Bar는 일치하지 않습니다. 이 문제를 어떻게 해결할 수 있습니까?Android의 사용자 정의 작업 표시 줄이 상위 항목과 일치하지 않습니다.

도움을 주셔서 감사합니다.

enter image description here

편집 1 : 내 주요 활동 XML, 그것은 작업 표시 줄에 아무 관심이 없어하지만 난 문제가 어디 있는지 알아낼 수

activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity" 
android:id="@+id/hh"> 

    </RelativeLayout> 

내 맞춤 작업 모음 XML 파일은 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button" 
    android:background="#ffff2301" /> 
</RelativeLayout> 

내 Java 코드;

public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    getSupportActionBar().setCustomView(R.layout.action_bar); 
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
} 

} 
+0

하는 유 업로드 할 수 주요 활동 xml? – Mann

+0

그리고 검은 공간이 메뉴 버튼을위한 공간이라고 생각합니다. – Mann

+0

감사합니다. 내 질문을 편집했습니다. 그리고 행동 바에 대해 우려되는 것을 발견하지 못합니다. – salih

답변

2

테마가 제공하는 기본 도구 모음에서 setCustomView()을 사용하고 있습니다. 사용자 정의보기는 이 아닌이 다른 도구 모음 공간 (로고, 제목, 오버플로 메뉴 ..)에 의해 사용 된 도구 모음 공간에로드됩니다.

사용자 지정 레이아웃이 도구 모음의 부분 인이되어 대체하지 않습니다. 그래서 어느 쪽이든 :

  1. 당신은 이런 식으로하고 싶습니다. 이 경우 문제는 배경색 일뿐입니다. 사용자 지정보기를 노란색으로 설정하는 방법을 모르지만 RelativeLayoutandroid:background="@color/transparent"을 추가하고 전체 도구 모음 색을 노란색으로 바꿔보십시오. 왼쪽의 공간에는 탐색 아이콘이로드되므로 원하는 위치에 놓을 수 있습니다.

  2. Toolbar API를 사용하면 사용자 지정보기를 쉽게 추가 할 수 있습니다. 이것은이 방식으로 수행됩니다에서 custom_toolbar.xml에,

    <android.support.v7.widget.Toolbar 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        xmlns:abc="http://schemas.android.com/apk/res-auto" 
        android:id="@+id/toolbar" 
        android:layout_height="?attr/actionBarSize" 
        android:layout_width="match_parent"> 
    
        <!-- you can add any custom view here --> 
    
        <Button 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="New Button" 
         android:id="@+id/button" 
         android:background="#ffff2301" /> 
    
    </android.support.v7.widget.Toolbar> 
    

    을 다음

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <include layout="@layout/custom_toolbar"/> 
    
    
        <RelativeLayout android:id="@+id/main_content" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
    
        </RelativeLayout> 
    
    </LinearLayout> 
    

그리고 당신의 onCreate() 지금 전화해야 :

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar tb = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(tb); 
    } 

    } 
+0

주셔서 감사합니다. 첫 번째 XML 파일은 내 주요 활동 XML이 될까요? – salih

+0

당신은 매력 bro와 같은 그것을했다 :) 고마워. 하지만 궁금하네요. 제목 표시 줄을 어떻게 설정할 수 있습니까? – salih

+0

'android : layout_gravity = "center"속성을 가진 textview를 추가하기 만하면,이 질문은 [전에] (http://stackoverflow.com/a/26548766/4288782) – natario

관련 문제