2017-05-26 61 views
0

我需要在navigationDrawer活動的工具欄的右側設置一個imageView和Textview。但問題是imageview和textviews的值是不同的片段活動。此外,我還需要使工具欄變得透明。我正在使用默認導航抽屜活動。任何人都可以幫忙嗎?任何幫助將不勝感激。如何從工具欄中的片段活動設置圖像和文本android

app_bar_main.xml文件

<? xml version = "1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.unitybees.photo.MainActivity"> 
    <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay" 
      android:background="@null"> 
     <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay"> 
      <LinearLayout 
       android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal"> 
       <ImageView android:id="@+id/right_icon" 
         android:layout_gravity="right" 
         android:layout_width="40dp" 
         android:layout_height="40dp"/> 
       <TextView android:id="@+id/right_text" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="right" 
         android:textColor="#000000"/> 
      </LinearLayout> 
     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.AppBarLayout> 
    <include layout="@layout/content_main"/> 
</android.support.design.widget.CoordinatorLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity implements 
     NavigationView.OnNavigationItemSelectedListener { 
    public static final int right_image = 1; 
    public static final int right_texts = 2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     TextView t = (TextView) findViewById(R.id.right_text); 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, 
       toolbar, R.string.navigation_drawer_open, 
       R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 
     NavigationView navigationView = (NavigationView) 
       findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     this.setTitle(""); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     displaySelectedScreen(item.getItemId()); 
     return true; 
    } 

    private void displaySelectedScreen(int itemId) { 
     android.support.v4.app.Fragment fragment = null; 
     switch (itemId) { 
      case R.id.nav_menu1: 
       fragment = new Book_now(); 
       break; 
      case R.id.nav_menu2: 
       fragment = new Payment(); 
       break; 
      case R.id.nav_menu3: 
       fragment = new Account(); 
       break; 
     } 
     if (fragment != null) { 
      FragmentTransactionft = getSupportFragmentManager().beginTransaction(); 
      ft.replace(R.id.content_frame, fragment); 
      ft.commit(); 
     } 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } 
} 

我Account.java

public class Account extends android.support.v4.app.Fragment { 
    TextView txt; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.account, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     getActivity().setTitle(""); 
    } 
} 
+1

你有什麼迄今所做實現這一目標在你的活動

public void initToolBar(String title) { toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(title); setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.mipmap.ic_launcher_round); toolbar.setNavigationOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "clicking the toolbar!", Toast.LENGTH_SHORT).show(); } } ); } 

2)要更新您的通話片段的方法將這個方法? – warl0ck

+0

先生,我編輯了我的問題。請看看 – Anu

+0

類似的問題https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android看看這是否有助於 – warl0ck

回答

0

您好試試這個,如果它可以幫助

1)使用mehtod

((MainActivity) getActivity()).initToolBar("Fragment Title"); 
+0

謝謝。我會查一下。 – Anu

+0

是的,試試吧,告訴你是否需要更多的幫助 –

+0

它的作品:)。我還有一個qstn,我如何設置圖像大小。而且我還需要在工具欄的右側添加圖像和文本。我需要從工具欄右側刪除3個點。你能幫我嗎? – Anu

相關問題