1

如何将名为Reload的按钮与我的操作栏右侧对齐?我知道这是可能我已经tryed把安卓重力= “右”,但没有工作Android Actionbar align button right

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); 

} 
} 

enter image description here

是的,它看起来像IOS我知道Android的定制权?

回答

2

您不需要LinearLayout。坚持以RelativeLayout的,并使用Android:layout_alignParentRight = “真”

看看这个答案,显示了一个很好的例子: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
+0

我知道什么ü意味着sortof ... u能提交我的XML代码的编辑用它编辑像你谈论? –

1

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

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

位于顶级视图或容器。

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

+0

+1表示研究的简单方法 –

1

你应该做这样的事情:

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