6

Tôi đã tạo một ListView với các mục có thể chọn nhưng ActionMode không hiển thị đúng. (Có và một nút "so sánh" menu trên đúng với màu trắng)Android: Style ActionMode trên AppCompat-v7 với Thanh công cụ

enter image description here

Tôi cố gắng để tạo kiểu cho actionMode với đoạn mã sau nhưng có gì thay đổi. Bất kỳ ý tưởng tại sao điều này có thể xảy ra? Tôi phát hiện ra rằng nếu tôi thiết lập màu nền trực tiếp trên thanh công cụ phụ tùng thay vì DarkTheme.ActionBar màu arround văn bản trên actionMode đã biến mất nhưng vẫn là màu sắc của văn bản là màu trắng và tôi cũng cần phải có màu sắc của ActionBar được xác định trên chủ đề thay vì tiện ích.

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/action_bar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    app:theme="@style/DarkTheme.ActionBar" 
    app:popupTheme="@style/DarkTheme.Popup"/> 

mytheme.xml

<resources> 

<style name="DarkTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item> 
    <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item> 
    <item name="android:textColorSecondary">#ffff8800</item> 
    <item name="android:textAllCaps">false</item> 
    <item name="android:background">#303030</item> 
</style> 

<style name="DarkTheme.Popup" parent="ThemeOverlay.AppCompat.Dark"> 
    <item name="android:textColor">#ffffff</item> 
</style> 

<style name="DarkTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="android:textColor">#101010</item> 
    <item name="android:textColorLink">#ff0099cc</item> 
    <item name="windowActionModeOverlay">true</item> 
    <item name="android:colorPrimaryDark">#000000</item> 
    <item name="android:navigationBarColor">#000000</item> 
    <item name="android:textAllCaps">false</item> 
    <item name="android:actionModeStyle">@style/DarkTheme.ActionMode</item> 
</style> 

<style name="DarkTheme.ActionMode" parent="Widget.AppCompat.ActionMode"> 
    <item name="android:actionModeBackground">@android:color/black</item> 
    <item name="android:background">#000000</item> 
    <item name="android:backgroundSplit">#000000</item> 
</style> 

</resources> 

Trả lời

-2

Tôi không thực sự hiểu công cụ của bạn, có vẻ kỳ lạ. Chủ đề dành cho Hoạt động hoặc Ứng dụng trong tệp kê khai. Đối với widget bạn nên sử dụng kiểu dáng. Khi nhìn vào câu hỏi của bạn, tôi nghĩ, bạn chỉ không biết cách sử dụng chủ đề và chủ đề là gì. Vì vậy, chủ đề của bạn nên trông giống như một công cụ như thế:

<resources> 
     <style name="AppBlankTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
      <!-- Main theme colors --> 
      <!-- your app branding color for the app bar --> 
      <item name="colorPrimary">@color/primary</item> 
      <!-- darker variant for the status bar and contextual app bars --> 
      <item name="colorPrimaryDark">@color/primary_dark</item> 
      <!-- theme UI controls like checkboxes and text fields --> 
     <item name="colorAccent">@color/accent</item> 
    </style> 
    <!--Both themes below are those accepted to make the ToolBar works--> 
    <!-- Base application theme. --> 
    <style name="AppTheme" parent="AppBlankTheme"> 
     <!-- Customize your theme here. --> 
     <!-- Base application theme. --> 
      <item name="windowNoTitle">true</item> 
      <item name="windowActionBar">false</item> 
    </style> 
    <style name="MyAppTheme" parent="Theme.AppCompat.NoActionBar"/> 
<!--The Theme for the Actvity that have actionMode--> 
    <style name="ActionModeAppTheme" parent="AppTheme"> 
     <item name="windowActionModeOverlay">true</item> 
     <item name="actionModeBackground">@color/primary_dark</item> 
     <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item> 
    </style> 
</resources> 

Sau đó bạn manifest bạn trông giống như một cái gì đó như thế:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.android2ee.formation.lollipop.toolbar" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".sample.ActivityWithItems" 
      android:label="@string/title_activity_activity_with_items" 
      android:parentActivityName=".MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.android2ee.formation.lollipop.toolbar.MainActivity" /> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="com.android2ee.formation.lollipop.toolbar.EXAMPLE" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".sample.ActionModeActivity" 
      android:label="@string/title_activity_actionmode" 
      android:theme="@style/ActionModeAppTheme" 
      android:parentActivityName=".MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.android2ee.formation.lollipop.toolbar.MainActivity" /> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="com.android2ee.formation.lollipop.toolbar.EXAMPLE" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

Và hoạt động của bạn trông giống như một cái gì đó như thế:

public class ActionModeActivity extends AppCompatActivity { 
    ActionMode mMode; 
    /** 
    * The action Bar 
    */ 
    private ActionBar actionBar; 
    private Toolbar toolbar; 
    Callback actionModeCallBack; 
    private boolean postICS,postLollipop; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState); 
     //You could also hide the action Bar 
//   getSupportActionBar().hide(); 
     setContentView(R.layout.activity_action_mode); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setNavigationIcon(R.drawable.ic_action_custom_up); 
     postICS =getResources().getBoolean(R.bool.postICS); 
     postLollipop =getResources().getBoolean(R.bool.postLollipop); 
     if(postLollipop){ 
      toolbar.setElevation(15); 
     } 

     setSupportActionBar(toolbar); 
     actionBar=getSupportActionBar(); 
     actionBar.setDisplayUseLogoEnabled(false); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     // Show the Up button in the action bar. 
     findViewById(R.id.start_actionmode).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       enableActionMode(); 
      } 
     }); 
     findViewById(R.id.stop_actionmode).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mMode != null) { 
        //To quit the ActionMode 
        mMode.finish(); 
       } 
      } 
     }); 


     actionModeCallBack=new Callback() { 
      @Override 
      public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
       return false; 
      } 

      @Override 
      public void onDestroyActionMode(ActionMode mode) { 
      } 

      @Override 
      public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
       getMenuInflater().inflate(R.menu.action_mode, menu); 
       return true; 
      } 

      @Override 
      public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
       Toast.makeText(ActionModeActivity.this, "Got click: " + item, Toast.LENGTH_SHORT).show(); 
       mode.finish(); 
       return true; 
      } 
     }; 
    } 

    private void enableActionMode() { 
     mMode = startSupportActionMode(actionModeCallBack); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_activity_with_items, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      // This ID represents the Home or Up button. In the case of this 
      // activity, the Up button is shown. Use NavUtils to allow users 
      // to navigate up one level in the application structure. For 
      // more details, see the Navigation pattern on Android Design: 
      // 
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
      // 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 
Các vấn đề liên quan