【问题标题】:How to Set Icon to display on the far right in Java如何在Java中将图标设置为显示在最右侧
【发布时间】:2017-01-26 23:43:53
【问题描述】:
getSupportActionBar().setLogo(R.drawable.addicon);

上面的代码似乎将我的 Icon 放在了 Center 中。我想放在最右边并使其也可点击。目前,图标的图像作为可绘制对象存在于项目文件中,我尚未将其包含在任何 xml 文件中。

【问题讨论】:

  • 您需要创建自定义工具栏,其中包含图像视图

标签: java android android-studio mobile


【解决方案1】:

你需要三样东西:

  1. 自定义工具栏布局
  2. 在您的活动中包含工具栏
  3. 将徽标设置为工具栏

创建新的资源布局: 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>

【讨论】:

    【解决方案2】:

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

    1. Option Menus
    2. 创建自定义工具栏。

    带有选项菜单

    在 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.
            }
        });
    

    快乐编码。

    【讨论】:

      【解决方案3】:

      您可以在工具栏 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);
          }
        }
      

      【讨论】:

        猜你喜欢
        • 2014-04-29
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 2020-05-23
        • 2014-04-14
        相关资源
        最近更新 更多