2016-10-08 3 views
1

Я пытаюсь добавить кнопку рядом с HealthyApp, но не повезло.Добавить кнопку в панели действий Android

Это мой исходный код и изображения

final ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

enter image description here

Я хочу, чтобы добавить кнопку удаления рядом HealthyApp, но название HealthyApp ушел.

final ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setDisplayShowHomeEnabled(false); 
     actionBar.setDisplayShowTitleEnabled(false); 
     LayoutInflater mInflater = LayoutInflater.from(this); 
     View mCustomView = mInflater.inflate(R.layout.header_button, null); 
     Button mTitleTextView = (Button) mCustomView.findViewById(R.id.title_text); 
     mTitleTextView.setText("Delete"); 
     actionBar.setCustomView(mCustomView); 
     actionBar.setDisplayShowCustomEnabled(true); 

enter image description here

delete_task

<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:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#d9d9d9" 
     android:minHeight="?attr/actionBarSize"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:gravity="right" 
      android:layout_height="wrap_content"> 

      <Button 
       android:text="Delete" 
       android:layout_width="wrap_content" 
       android:background="#000" 
       android:textColor="#fff" 
       android:layout_height="wrap_content" 
       android:textSize="16dp" /> 
     </LinearLayout> 

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

    <ImageView 
     android:src="@mipmap/to_do" 
     android:layout_marginTop="50dp" 
     android:layout_width="130dp" 
     android:layout_height="210dp" 
     android:id="@+id/imageView" 
     android:gravity="center" 
     android:layout_centerHorizontal="true"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="20dp" 
     android:text="No List Found" 
     android:textSize="15dp" 
     android:textColor="@color/btn_login" 
     android:gravity="center" 
     android:id="@+id/NoData" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/imageView"/> 

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

     <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/list_todo" 
      android:layout_weight="1" 
      android:layout_alignParentLeft="true" /> 

    </RelativeLayout> 

</RelativeLayout> 

enter image description here

ответ

3

Добавить панель инструментов в вашем XML. и используйте в своей теме действия NoActionBar.

добавить эти панели инструментов в вашем XML на верхних

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#131313" 
    android:minHeight="?attr/actionBarSize"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:background="#00aaaaaa" 
     android:layout_gravity="right" 
     android:layout_height="match_parent"> 

     <Button 
      android:text="Delete" 
      android:layout_width="wrap_content" 
      android:background="#000" 
      android:textColor="#fff" 
      android:layout_height="wrap_content" 
      android:textSize="16dp" /> 

    </LinearLayout> 

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

добавить эти темы в вашей деятельности в Menifest.xml

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

и добавить этот код в вашей деятельности ....

Toolbar topToolBar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(topToolBar); 
ActionBar actionBar = getSupportActionBar();; 
actionBar.setDisplayHomeAsUpEnabled(true); 

Выход: -

fg

ИЛИ

Я думаю, что это возможно только значок .....

использовать этот пункт в menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/delete" 
     android:icon="@drawable/YourImage" 
     app:showAsAction="always"></item> 
</menu> 

Примечание: - используйте android:icon="@drawable/YourImageWithTextDelete". магу .....

+0

но я не хочу использовать значок, просто андроидную кнопку – Hoo

+0

, тогда вы должны использовать пользовательскую панель инструментов ..... включить панель инструментов внутри вашего xml .... и использовать «NoActionBar» в своей теме активности ... .. – sushildlh

+0

ОК, я буду искать его. Thnaks – Hoo

2

В макете активность

<android.support.v7.widget.Toolbar 
     android:layout_width="match_parent" 
     android:background="@color/YourColor" 
     android:layout_height="?attr/actionBarSize" 
     app:layout_collapseMode="pin" 
     android:theme="@style/about_toolbar" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/YourText" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/YourText" 
     /> 

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

В вашей деятельности

public class YourActivity extends ActionBarActivity { 

    private Toolbar toolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.your_acticity_layout); 

     // Set a Toolbar to replace the ActionBar. 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle(""); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     } 

     // ... your other methods 
} 

В манифесте, активность тег

<activity 
     android:name=".YourActivity" 
     android:label="@string/YourActivityString" 
     android:theme="@style/YourTheme" /> 

В вашем style.xml

<style name="YourTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">...</item> <!-- declare your styles --> 
</style> 

Если вам нужен лучший пример кода: просто скачайте https://github.com/chrisbanes/cheesesquare, у него есть пример панели инструментов.

Смежные вопросы