2016-07-14 92 views
4

Ich verwende nicht Toolbar oder ActionbarActivity. Ich verwende einfache Aktivität. Meine Aktivität hat eine Aktionsleiste. Ich möchte es so anpassen, dass der Titel der Aktivität in der Mitte ausgerichtet wird und es gibt eine Zurück-Schaltfläche mit etwas Text. Wie dieses BildZurück-Taste mit Text und Titel in Android-Aktionsleiste

enter image description here

. Ich benutze ein benutzerdefiniertes Layout für meine Aktionsleiste und das Design ist in Ordnung, die Rücktaste funktioniert auch. Aber das Problem ist der Text mit Zurück-Taste ist anklickbar.

Hier ist, was ich

getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getActionBar().setCustomView(R.layout.custom_actionbar_title); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

Das Layout für custom_actionbar_title

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="16.0" 
    android:weightSum="16.0"> 


    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:text="Incidents" 
     android:gravity="left" 
     android:layout_weight="5" 
     android:textColor="#ffffff" 
     android:id="@+id/backButtonText" 
     android:textSize="18sp" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textAlignment="center" 
     android:layout_weight="6" 
     android:text="Detail" 
     android:gravity="center" 
     android:textColor="#ffffff" 
     android:id="@+id/title" 
     android:textSize="18sp" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:layout_weight="5" 
     android:layout_gravity="end"/> 

</LinearLayout> 
+0

http://stackoverflow.com/questions/10108774/how-to-implement-the-android-actionbar- Zurück-Button werfen Sie einen Blick –

Antwort

1

Sie kann entsprechend selbst versucht haben, bearbeiten:

eine separate xml machen: Lassen Sie uns sagen app_bar 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:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    android:minHeight="?attr/actionBarSize"> 


    <LinearLayout 
     android:id="@+id/ll_back_image" 
     android:layout_width="50dp" 
     android:layout_height="48dp" 
     android:visibility="invisible" 
     android:gravity="left"> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:icon="@mipmap/back" 

      android:layout_gravity="center" 
      android:gravity="end|center" 
      android:id="@+id/toolbar_back_image"/> 

    </LinearLayout> 



    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:layout_gravity="center" 
     android:textSize="17dp" 
     android:textStyle="bold" 
     android:textColor="#fff" 

     android:id="@+id/toolbar_title"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:visibility="invisible" 
     android:layout_gravity="right" 
     android:icon="@mipmap/ic_sort_white_24dp" 
     android:id="@+id/filter_img_id"/> 
</android.support.v7.widget.Toolbar> 

Fügen Sie die oben Layout in Ihrem main_activity.xml // Sie nach Ihren xml ändern können: wie folgt aus:

<?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" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:background="@color/faintwhite" 
    android:id="@+id/container"> 
<RelativeLayout 

    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/app_bar"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_gravity="center" 

     android:layout_below="@+id/tool_bar"> 
    <LinearLayout 
     android:id="@+id/lin_lay" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 


      /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linear2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/img" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      /> 
    </LinearLayout> 
    </LinearLayout> 

</RelativeLayout> 
</FrameLayout> 

Wenn Sie Fragment verwenden:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.mini_detail, container, false); 
     findAllId(); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      Window window = getActivity().getWindow(); 
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
      window.setStatusBarColor(MiniDetail.this.getResources().getColor(R.color.colorPrimary)); 
     } 
     ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);// action bar 
     ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false); 
     TextView tv= (TextView) toolbar.findViewById(R.id.toolbar_title); 
     tv.setText("MUMBA"); 

wenn Sie mit Aktivität:

public class SplashScreen extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash_screen); 
     Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      Window window = getWindow(); 
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
      window.setStatusBarColor(SplashScreen.this.getResources().getColor(R.color.black)); 
     } 
     toolbar.setBackgroundColor(getResources().getColor(R.color.black)); 
     setSupportActionBar(toolbar);// action bar 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
     TextView tv= (TextView) toolbar.findViewById(R.id.toolbar_title); 
     tv.setText("Welcome"); 
0
<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="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

    "your back button & textview code here" 

    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout>