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