【问题标题】:How to display both of Icon and title in android menu如何在android菜单中同时显示图标和标题
【发布时间】:2015-08-25 12:12:56
【问题描述】:

我想知道如何在 android 菜单中同时显示图标和标题。

我正在使用这个:

<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.example.yasser.version6.NewprofileActivity">


    <item
        android:id="@+id/action_done"
        android:title="SUIVANT"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_done"
        app:showAsAction="always|withText" />


</menu>

这是我的活动,我正在扩展 AppCompatActivity 好吗?然后我正在为我的应用程序使用自定义工具栏

public class NewprofileActivity extends AppCompatActivity {
    ImageView profil_image;
    Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newprofile);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("Profil");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        profil_image=  (ImageView) findViewById(R.id.profil_image);


        Picasso.with(profil_image.getContext()).load(R.drawable.profil_icon).transform(new Imagetransformation()).into(profil_image);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_newprofile, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

但标题只在横向显示

【问题讨论】:

标签: android menu menuitem


【解决方案1】:

使用app:showAsAction="ifRoom|withText" 代替app:showAsAction="always|withText"

还有

getSupportActionBar().setIcon(R.drawable.Your_Icon);

【讨论】:

  • 我收到一个错误“应该为 appcompat 库使用 app:showAsAction
  • @YasserB。现在设置这个应用程序:showAsAction="ifRoom|withText"
  • 同样没有变化,我更新了帖子,所以你可以看看我的活动
  • @YasserB。 getSupportActionBar().setIcon(R.drawable.Your_Icon);
  • 如果不起作用,请更新您的主题,例如 @drawable/your_logouseLogo|showHome
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多