1

我一直在努力尝试创建自定义 ABS 布局,但无济于事。基本上,我正在尝试向菜单添加一个按钮,单击该按钮会执行某些操作(现在,它只是说“成功单击”。这是我的实现:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    ActionBar bar = getSupportActionBar();
    bar.setTitle("Main Screen");
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000FF")));

    View cView = getLayoutInflater().inflate(R.layout.custom_action_bar, null);
    bar.setCustomView(cView);
    ImageView actionBarLogo = (ImageView) cView.findViewById(R.id.actionBarLogo);

    actionBarLogo.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Toast.makeText(DashboardActivity.this.getApplicationContext(),"Successfully clicked.", Toast.LENGTH_LONG).show();
        }
    });
}

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:orientation="horizontal" >

<ImageView
    android:id="@+id/actionBarLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:paddingBottom="50dp"
    android:paddingLeft="10dp"
    android:paddingRight="50dp"
    android:paddingTop="50dp"
    android:scaleType="centerCrop"
    android:src="@drawable/harddisk"/>

</LinearLayout>

这里有一个解决方案:Actionbarsherlock custom layout clickable imageview 我试过了,但是按钮仍然没有出现在操作栏中。有任何想法吗?

4

0 回答 0