当我使用setHomeButtonEnabled(true)时,App图标不可点击

时间:2017-04-05 01:02:10

标签: android

当我设置“setHomeButtonEnabled(true)”时,但应用程序图标无法在模拟器上单击。我想知道这个图标不起作用吗?

接下来是Main2Activity文件

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <h1>Hello Plunker!</h1>
    <div style="height:100px;width:100px; background-color:blue" draggable="true"></div>
    <input disabled draggable="true"/>
    <script>

    </script>
  </body>


</html>

这是主要文件

    package com.example.laoban.newtext;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class Main2Activity extends Activity {
    private TextView txt;
    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        txt = (TextView)findViewById(R.id.text);
        actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        // actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem mi)
    {
        if(mi.isChecked())
        {
            mi.setChecked(true);
        }
        switch(mi.getItemId())
        {
            case android.R.id.home:
                Log.d("Main2Activity","actionBar.setHomeButtonEnabled");
                txt.setText("哈哈");
                break;
        }
        return true;
    }
    public void show(View source)
    {
        actionBar.show();

    }
    public void hide(View source)
    {
        actionBar.hide();
    }
}

<?xml version="1.0" encoding="utf-8"?>

请帮助我,我会为你付钱

1 个答案:

答案 0 :(得分:0)

  1. 使用actionBar.setDisplayHomeAsUpEnabled(true)
  2. 尝试使用AppCompatActivity代替Activity
  3. 使用任何AppCompat主题(例如Theme.AppCompat.Light.DarkActionBar
  4. 这是工作代码。试试这个:

    <强> Main2Activity.java:

    package com.example.laoban.newtext;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.TextView;
    
    public class Main2Activity extends AppCompatActivity {
        private TextView txt;
        ActionBar actionBar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            txt = (TextView) findViewById(R.id.text);
    
            actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        public boolean onCreateOptionsMenu(Menu menu)
        {
            MenuInflater inflater = new MenuInflater(this);
            inflater.inflate(R.menu.menu_main,menu);
    
            return super.onCreateOptionsMenu(menu);
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem mi)
        {
            if(mi.isChecked())
            {
                mi.setChecked(true);
            }
            switch(mi.getItemId())
            {
                case android.R.id.home:
                    Log.d("Main2Activity","actionBar.setHomeButtonEnabled");
                    txt.setText("哈哈");
                    break;
            }
            return true;
        }
        public void show(View source)
        {
            actionBar.show();
    
        }
        public void hide(View source)
        {
            actionBar.hide();
        }
    }
    

    <强> styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">#3F51B5</item>
            <item name="colorPrimaryDark">#303F9F</item>
            <item name="colorAccent">#FF4081</item>
        </style>
    </resources>
    

    <强>输出

    enter image description here