【问题标题】:moving an Image Button in the action bar在操作栏中移动图像按钮
【发布时间】:2014-11-16 13:32:15
【问题描述】:

我正在创建一个自定义操作栏。 它看起来不错,但我有一个奇怪的问题,由于某种原因,右侧的 ImageButton 卡住了,我似乎无法用边距移动它。 这是我的布局 - custom_actionbar.xml:

<ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher"/>

<ImageButton
    android:id="@+id/drawerButton"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="8dp"
    android:src="@drawable/drawer_button"
    android:background="@null"/>

<ImageButton
    android:id="@+id/favouritesButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:background="@null"
    android:src="@drawable/ic_action_important" />

还有我的 MainActivity.java :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);

    ImageButton drawer = (ImageButton) mCustomView.findViewById(R.id.drawerButton);
    drawer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Drawer Clicked", Toast.LENGTH_LONG).show();
        }
    });

    ImageButton favButton = (ImageButton) mCustomView.findViewById(R.id.favouritesButton);
    favButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Fav Clicked", Toast.LENGTH_LONG).show();
        }
    });

    actionBar.setCustomView(mCustomView);
    actionBar.setDisplayShowCustomEnabled(true);
}

您可以点击下面的链接查看现在的情况。

picture of how it is right now

我需要将“星”图标向左移动 8dp。 边距在导航抽屉图标中有效,但由于某种原因它不适用于正确的图标。 他离屏幕右侧太近了。

有人知道怎么解决吗?

非常感谢,Refael。

【问题讨论】:

    标签: android android-layout android-actionbar


    【解决方案1】:

    在设置自定义视图的时候尽量指定视图的布局参数,像这样

    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
    actionBar.setCustomView(mCustomView, layoutParams); 
    

    如果这不起作用,请尝试在自定义视图的相对布局中指定此

    android:layout_gravity="fill_horizontal"
    

    【讨论】:

    • 嗨 Joao,这似乎不起作用,LayoutParams 似乎出错“找不到符号类 LayoutParams”。和“layout_gravity”,也试过了,也没有做任何不同的事情。
    • 对不起,你是对的。我忘了在那里添加父类。我编辑了我的答案。再试一次。
    • 您好 Joao,非常感谢您的帮助。尽管它仍然无法正常工作。 logcat 中的错误 - “方法 ActionBar.setCustomView 不适用
    • 我没有意识到 setCustomView 需要 ActionBar.LayoutParams。再次,我编辑了我的答案。再试一次:) 导入时要小心。如果你使用支持库导入android.support.v7.app.ActionBar,否则导入android.app.ActionBar
    • 亲爱的 Joao Sousa 先生.. 谢谢!问题已解决,感谢您!我真的很感谢它,伙计,干杯!
    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2019-03-06
    相关资源
    最近更新 更多