如何设置要在Java中最右侧显示的图标

时间:2016-09-19 10:10:39

标签: java android android-studio mobile

getSupportActionBar().setLogo(R.drawable.addicon);

上面的代码似乎将我的Icon放在中心。我想放在最右边,也可以点击它。目前,图标的图像在项目文件中作为drawable我没有将它包含在任何xml文件中。

3 个答案:

答案 0 :(得分:0)

您可以在工具栏xml中设置android:layoutDirection="rtl",以便将其放置在右侧和

设置以下代码可点击:

getSupportActionbar().setDisplayHomeAsUpEnabled(true);

和:

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        //Do stuff
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
  }

答案 1 :(得分:0)

有两种方法可以做到这一点。

  1. Option Menus
  2. 创建自定义工具栏。
  3. 使用选项菜单

    在res / menu下创建main.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.ioa.MainActivity" >
    
        <item
            android:id="@+id/action_addition"
            android:icon="@drawable/addicon"
            android:orderInCategory="100"
            android:title="Addition"
            app:showAsAction="always"/>
    
    </menu>
    

    并在您的活动中创建菜单,例如

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    您可以对特定菜单项执行操作。

    @Override
    public boolean onOptionsItemSelected(MenuItem Item) {
    
        if (Item.getItemId() == R.id.action_addition) {
            // perform action
        }
        return super.onOptionsItemSelected(paramMenuItem);
    }
    

    使用自定义工具栏

    像这样创建ToolBar

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <ImageView
                android:id="@+id/add"
                android:layout_width="46dp"
                android:layout_height="46dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="4dp"
                android:clickable="true"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
    

    现在您可以执行特定ImageView的点击事件。

    ImageView add = (ImageView) findViewById(R.id.add);
    
    add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do work.
            }
        });
    

    快乐的编码。

答案 2 :(得分:0)

你需要3件事:

  1. 自定义工具栏布局
  2. 在活动中包含工具栏
  3. 将徽标设置为工具栏
  4. 创建新的资源布局: include_toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="?attr/colorPrimaryDark"
        android:id="@+id/include_toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    

    更改您的活动文件:MainActivity.java

    public class BaseActivity extends AppCompatActivity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar)findViewById(R.id.include_toolbar); 
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap_ic_launcher);
    }
    }
    

    这是styles.xml:

    <!-- language: xml-->
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
            <color name="colorPrimary">#3F51B5</color>
        </resources>