2017-07-17 11 views
0

我在AppCompatActivity中使用自定义操作栏。我给出了我的代码以及投手。我尝试了所有可用的堆栈溢出解决方案。但直到我无法解决这个问题。请帮助我。如何减少图标和自定义操作栏中的按键之间的空间android

action_toolbar.xml

<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" 
style="@style/toolBarTheme" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="?attr/actionBarSize" 
app:contentInsetStart="0dp" 
app:theme="@style/AppTheme.AppBarOverlay"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/appLogo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/app_name" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/tvToolbarTitle" 
     style="@style/textViewTheme" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/appLogo" 
     android:layout_toEndOf="@+id/appLogo" 
     android:layout_marginLeft="14dp" 
     android:layout_marginStart="14dp" /> 
</RelativeLayout> 

enter image description here

回答

1

你可以使用toolbar属性用于此

app:contentInsetLeft="0dp" 
app:contentInsetStart="0dp" 
app:contentInsetStartWithNavigation="0dp" 

这样使用工具栏

<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" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    app:contentInsetStartWithNavigation="0dp" /> 
+0

它的工作良好..感谢帮助... –

+0

@GowsikRaja最受欢迎 –

0

如果您使用Toolbar然后加入Toolbar以下行xml

app:contentInsetStart="0dp" 

更新:

你的意思是你想要像Whatsapp的工具栏。但它不是通过使用App-compat支持库工具栏您必须使用自定义操作栏。

你可以得到以下结果如图所示的图片下面

enter image description here

使用下面的代码。

activity.xml

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_chats" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 
      <include layout="@layout/custom_toolbar"/> 
</android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

custom_toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="?attr/selectableItemBackgroundBorderless" 
android:layout_width="fill_parent" 
android:layout_height="?actionBarSize" 
> 
<!-- android:background="?attr/selectableItemBackgroundBorderless" will cause this Custom View to make ripple effect --> 

<LinearLayout 
    android:id="@+id/conversation_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:contentDescription="@string/abc_action_bar_up_description" 
    android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/conversation_contact_photo" 
      android:layout_width="35.0dip" 
      android:layout_height="35.0dip" 
      android:src="@drawable/icon" 
      android:scaleType="fitCenter" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@id/conversation_image" 
    android:orientation="vertical" 
    android:paddingBottom="2.0dip" 
    android:paddingLeft="4.0dip" 
    android:paddingRight="0.0dip" 
    android:paddingTop="0.0dip" > 


    <TextView 
     android:id="@+id/action_bar_title_1" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_gravity="center_vertical" 
     android:layout_marginLeft="6dp" 
     android:layout_weight="0.6" 
     android:ellipsize="end" 
     android:gravity="center_vertical" 
     android:maxLines="1" 
     android:textSize="18sp" 
     android:text="shanraisshan" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/action_bar_title_2" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_marginLeft="6dp" 
     android:layout_weight="0.4" 
     android:ellipsize="end" 
     android:text="last seen 1 hour ago" 
     android:maxLines="1" 
     android:textSize="12sp" /> 


</LinearLayout> 

YourActivity.java

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_chats); 
setSupportActionBar(toolbar); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
toolbar.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Log.e("Toolbar","Clicked"); 
    } 
}); 
+0

是,尝试这样做。看我的代码。要使用这个我的主要活动正常工作。但新的活动它不起作用 –

+0

你检查,它的决心与否? –

+0

很抱歉@Mehul现在只有我看到你的答案。但它不起作用。但上面的Ans工作正常,我去那。 Thankyou –

相关问题