【问题标题】:Android Actionbar align button rightAndroid Actionbar 右对齐按钮
【发布时间】:2015-11-27 22:40:24
【问题描述】:

如何将名为 Reload 的按钮对齐到我的操作栏右侧?我知道这是可能的

activity_main_ab.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="fill_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >

<Button
android:id="@+id/action_bar_button_about"
android:layout_width="fill_parent"
android:layout_height="match_parent"           
android:layout_weight="1"
android:background="@drawable/ios_btn"
android:textColor="#ffffff"
android:text="about" />

<Button
android:id="@+id/action_bar_button_reload"
android:layout_width="fill_parent"
android:layout_height="match_parent" 
android:background="@drawable/ios_btn"          
android:layout_weight="1"
android:textColor="#ffffff"
android:text="reload" />
</LinearLayout>    
</RelativeLayout>

这是我的 MainActivity.java

package jb.cydia;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   
    final ActionBar ab = getActionBar();
    ab.setDisplayShowHomeEnabled(false);
    ab.setDisplayShowTitleEnabled(false);     
    final LayoutInflater inflater =    (LayoutInflater)getSystemService("layout_inflater");
    View view = inflater.inflate(R.layout.activity_main_ab,null); 
    ab.setCustomView(view);
    ab.setDisplayShowCustomEnabled(true);

}
}

是的,看起来 IOS 我知道 androids 关于定制的权利??

【问题讨论】:

    标签: android-actionbar actionbarsherlock android-xml android-button


    【解决方案1】:

    您不需要 LinearLayout。坚持使用 RelativeLayout 并使用 android:layout_alignParentRight="true"

    看看这个答案,它展示了一个很好的例子:Android Layout Right Align

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="fill_horizontal"
    android:orientation="horizontal" >
    
    
    <Button
    android:id="@+id/action_bar_button_about"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"           
    android:layout_weight="1"
    android:background="@drawable/ios_btn"
    android:textColor="#ffffff"
    android:text="about" />
    
    <Button
    android:id="@+id/action_bar_button_reload"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:background="@drawable/ios_btn"          
    android:layout_weight="1"
    android:textColor="#ffffff"
    android:text="reload" 
    android:layout_alignParentRight="true"/>
    
    </RelativeLayout>
    

    这是我所做的:

    • 删除了线性布局
    • 已添加android:layout_alignParentRight="true"
    • 将 android:layout_width 和 layout_height 更改为 wrap_content

    【讨论】:

    • 我知道你的意思是什么...你可以提交我的 xml 代码的编辑,并像你所说的那样编辑它吗??
    【解决方案2】:

    ActionBar 自定义视图的父级似乎是 FrameLayout。因此,最快的解决方案是:

    android:layout_width="wrap_content"
    android:layout_gravity="end"
    

    在顶层视图或容器中。

    由于这对 ActionBar 的视图的实现做出了假设,因此 Enno 的视图可能更健壮。也就是说,我认为这是一个非常安全的假设。

    【讨论】:

      【解决方案3】:

      你应该这样做:

      actionBar.setCustomView(view, new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
           ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL));
      actionBar.setDisplayShowCustomEnabled(visible);
      

      【讨论】:

        猜你喜欢
        • 2021-05-10
        • 2019-10-05
        • 2017-06-13
        • 1970-01-01
        • 2020-01-06
        • 1970-01-01
        • 2022-12-14
        • 1970-01-01
        相关资源
        最近更新 更多