2015-04-29 31 views
1

我想在活動中實現自定義操作欄。我在下面這樣做了,但是讓我的自定義操作欄與另一個重疊。我附上了下面的屏幕截圖。自定義操作欄重疊其他元素

public class MainActivity extends ActionBarActivity { 

    private TextView actionBarTitle; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 


     ActionBar actionBar = getSupportActionBar(); 

     View customView=getLayoutInflater().inflate(R.layout.actionbar_layout, null); 
     actionBar.setCustomView(customView); 


     actionBarTitle =(TextView)findViewById(R.id.actionbar_text); 
     actionBarTitle.setText("Action bar"); 
     } 

} 

清單:

 <application 
     android:name="MyApplication" 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppBaseTheme" 
     android:requiredForAllUsers="false">   

     <activity android:name="MainActivity"  
       android:screenOrientation="portrait"/> 

    </application> 

actionbar_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 

     android:background="#ffffff" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/actionbar_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="32dp" 

      android:layout_alignParentTop="true" 
      android:src="@drawable/ic_launcher" /> 

     <TextView 
      android:id="@+id/actionbar_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:layout_marginTop="8dp" 
      android:layout_toRightOf="@+id/actionbar_icon" 
      android:text="title" /> 

    </RelativeLayout> 

的strings.xml:

<resources> 

    <string name="app_name">sample</string> 
</resources> 

styles.xml

<resources> 

     <!-- 
      Base application theme, dependent on API level. This theme is replaced 
      by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
     --> 
     <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 
      <!-- 
       Theme customizations available in newer API levels can go in 
       res/values-vXX/styles.xml, while customizations related to 
       backward-compatibility can go here. 
      --> 
     </style> 

     <!-- Application theme. --> 
     <style name="AppTheme" parent="AppBaseTheme"> 
      <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
     </style> 
    <style name="Dialog_No_Border"> 
      <item name="android:windowIsFloating">true</item> 
      <item name="android:windowBackground">@color/transparent_color</item> 
     </style> 

    </resources> 

如何刪除示例文本和黑色背景以便僅顯示自定義視圖?

enter image description here

+0

如何刪除示例文本和黑色背景..只顯示自定義視圖.. – user1862322

+0

(示例)文本和黑色的顏色欄必須消除和唯一的圖標和操作欄文本..... – user1862322

回答

0

你是顯示在你的屏幕截圖究竟是什麼規格說什麼是應該發生的。當您在操作欄中使用自定義視圖時,它將放置在操作欄左側的應用圖標(如果有)和操作欄右側的操作按鈕(如果有)之間。

自定義導航視圖顯示在應用程序圖標,任何操作按鍵之間,並且可以使用可用的有任何空間。

ActionBar#setCustomView(View view)

0

嘗試從Activity類擴展和使用動作條。

public class MainActivity extends Activity { 

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

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

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
    TextView mTitleTextView = (TextView)  mCustomView.findViewById(R.id.title_text); 
    mTitleTextView.setText("your title here"); 
actionBar.setCustomView(mCustomView); 
    actionBar.setDisplayShowCustomEnabled(true); 

它爲我工作!通過膨脹自定義的操作欄!但要確保它不會妨礙其他佈局或組件。

ps:我已經在我的一個項目中使用了它,並且運行良好,如果您正在處理任何錯誤/錯誤/建議,請更正。