2012-05-18 65 views
5

我一直试图在ICSAndroid的动作条setActionView布局问题

使用setActionView从动作条似乎应该是简单的,但不知何故我没有变,我会希望布局对齐。正如您在下面的图片中看到的,“目标”图标在其布局中正确居中。但是当我设置ActionBar(进度)时,无论我尝试什么,进度视图总是与右边对齐。

before clicking the menu item after clicking the menu item

这里有2个状态,之前并单击该菜单项后。正如你所看到的,进度视图总是与右侧对齐。我试着改变我的进度布局xml中的重力选项从左到右到中心,无论我做什么改变它似乎都没有改变任何东西。

我还没有找到任何关于这个问题的信息,所以我想我一定在做错事。

有没有人有线索? 感谢您的帮助!

这里是我的操作栏菜单布局 'action_bar_menu.xml'

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/locate" 
      android:title="locate" 
      android:icon="@drawable/locate" 
      android:showAsAction="ifRoom|withText" /> 
</menu> 

这里是我的进度布局 'inderterminate_progress.xml'

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center"> 

    <ProgressBar android:layout_width="25dp" 
       android:layout_height="25dp" 
       android:layout_gravity="center" 
       android:indeterminate="true" 
       style="?android:attr/progressBarStyleInverse"/> 
</FrameLayout> 

最后,这里是我testx活动

public class HelloAndroidActivity extends Activity { 

    /** 
    * Called when the activity is first created. 
    * @param savedInstanceState If the activity is being re-initialized after 
    * previously being shut down then this Bundle contains the data it most 
    * recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b> 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     getActionBar().setTitle("Test"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.action_bar_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     super.onOptionsItemSelected(item); 


     if (R.id.locate == item.getItemId()) { 

      final MenuItem menuItem = item.setActionView(R.layout.inderterminate_progress); 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        SystemClock.sleep(3000); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          menuItem.setActionView(null); 
         } 
        }); 
       } 
      }).start(); 
     } 

     return true; 
    } 
} 

回答

9

似乎明确定义了样式并在其中添加了4dip填充布局的根膨胀,如下图所示解决了这个问题

<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:paddingLeft="4dip" 
    android:paddingRight="4dip" 
    style="?android:attr/actionButtonStyle" /> 

这似乎在Android样式here

+0

是你是完全正确的:)。 Thx的帮助非常感谢。 –

3

使用有点晚了,但正确的解决方案是不是一个绝对的数值,如4DP。正确的做法是将minWidth设置为ABS值:

android:minWidth="@dimen/abs__action_button_min_width" 

例进度条:

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

<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" 
      android:minWidth="@dimen/abs__action_button_min_width" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:focusable="true" 
      android:layout_gravity="center" 
      style="@android:style/Widget.ProgressBar.Small"/>