2017-05-19 14 views
0

アンドロイドアプリ開発とアンドロイドスタジオについては非常に新しいです。 私はいくつかのカップルの日の周りを検索していますが、まだ解決策を見つけることはできません。Androidスタジオはさまざまな活動の異なるツールバーアイテムを持っています

どのように私は異なるツールバーを持つことができます異なる活動のアイテムは、それを行う方法ですか? 私はそれを見ることができるように例がありますか? その中にそのチュートリアルがありますか?

すでに多くのおかげで助けてください。

MainActivity.java

package com.example.stage.absa; 

import android.Manifest; 
import android.content.ClipData; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Toast; 
import com.google.zxing.Result; 
import me.dm7.barcodescanner.zxing.ZXingScannerView; 

public class MainActivity extends AbsRuntimePermission { 

    private ZXingScannerView scannerView; 
    private static final int REQUEST_PERMISSION = 10; 
    Toolbar toolbar; 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     requestAppPermissions(new String[]{ 
         Manifest.permission.CAMERA}, 
       R.string.msg, REQUEST_PERMISSION); 
    } 

    @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_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 

     if (id == R.id.action_settings) { 
      setContentView(R.layout.activity_settings); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void browser1(View view) { 
     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/")); 
     startActivity(browserIntent); 
    } 

    public void scanCode(View view) { 
     scannerView = new ZXingScannerView(this); 
     scannerView.setResultHandler(new ZxingScannerResultHandler()); 

     setContentView(scannerView); 
     scannerView.startCamera(); 
    } 

    class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler { 
     @Override 
     public void handleResult(Result result) { 
      String resultCode = result.getText(); 
      Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show(); 

      setContentView(R.layout.activity_main); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
      scannerView.stopCamera(); 
     } 
    } 

    @Override 
    public void onPermissionsGranted(int requestCode) { 
     //Do anything when permisson granted 
     Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show(); 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
android:orientation="vertical"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:minHeight="?attr/actionBarSize" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:titleTextColor="@android:color/white" 
    android:background="?attr/colorPrimary"> 
</android.support.v7.widget.Toolbar> 

<!-- Layout for content is here. This can be a RelativeLayout --> 
    <RelativeLayout 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" 
     tools:context="com.example.stage.absa.MainActivity"> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="browser1" 
      android:layout_margin="2dp" 
      android:src="@drawable/suppliance" /> 

     <Button 
      android:id="@+id/S" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:onClick="scanCode" 
      android:layout_margin="5dp" 
      android:backgroundTint="@color/colorPrimary" 
      android:text="Scan" /> 
    </RelativeLayout> 

</LinearLayout> 

SettingsActivity.java

package com.example.stage.absa; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.support.v7.widget.Toolbar; 
    import android.view.Menu; 


    public class SettingsActivity extends AppCompatActivity { 
     Toolbar toolbar; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_settings); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
     } 

     @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_settings, menu); 
      return true; 
     } 
    } 

activity_settings.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     app:titleTextColor="@android:color/white" 
     android:layout_height="?attr/actionBarSize"> 

     <ImageView 
      android:id="@+id/tv_header_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left" 
      android:layout_marginRight="20dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text=" Name" 
      android:textColor="#ffffff" 
      android:gravity="center" 
      android:id="@+id/brand_name" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:orientation="vertical" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="C Name :" 
       android:layout_gravity="right" 
       android:layout_marginRight="20dp" 
       android:textColor="#ffffff" 
       android:id="@+id/t1" /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="User Name :" 
       android:layout_gravity="right" 
       android:layout_marginRight="20dp" 
       android:textColor="#ffffff" 
       android:id="@+id/t2" /> 

     </LinearLayout> 



    </android.support.v7.widget.Toolbar> 

    <!-- Layout for content is here. This can be a RelativeLayout --> 
    <RelativeLayout 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" 
     tools:context="com.example.stage.absa.MainActivity"> 


     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="Settings" /> 
    </RelativeLayout> 

</LinearLayout> 

menu_settings.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <item 
     android:id="@+id/action_settings" 
     android:icon="@drawable/ic_action_back" 
     android:title="Back" 
     app:showAsAction="ifRoom" /> 
</menu> 

menu_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <item 
     android:id="@+id/action_settings" 
     android:icon="@drawable/ic_action_settings" 
     android:title="Settings" 
     app:showAsAction="ifRoom" /> 
</menu> 
+0

[Androidのツールバーのボタンを作成する]の可能複製(http://stackoverflow.com/questions/31231609/creating-a-button-in-android-toolbar) – Lepidopteron

答えて

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

が異なるだけな活動のために別のメニューリソースを渡すR.id.menu1またはR.menu.menu2、すなわち。

+0

に私はメニュー/ menu_settings.xmlを使用していませんが、レイアウト/ activity_settings.xmlを使用していますが、何が間違っている可能性がありますか? – Merijn

+0

両方のコードを投稿してください。 –

+0

完了外観ページの上部に!すでに助けてくれてありがとう。 – Merijn

0

のようなアクティビティに対してカスタムツールバーのレイアウトを設定できます。私はその前に既になく、試してみました

<android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       app:titleTextColor="@android:color/white" 
       android:layout_height="?attr/actionBarSize" 
       app:popupTheme="@style/AppTheme.PopupOverlay"> 

       <ImageView 
        android:id="@+id/tv_header_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="left" 
        android:layout_marginRight="20dp" 
        android:src="@drawable/siyaram_logo"/> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text=" Name" 
        android:textColor="#ffffff" 
        android:gravity="center" 
        android:id="@+id/brand_name" /> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:orientation="vertical" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="C Name :" 
         android:layout_gravity="right" 
         android:layout_marginRight="20dp" 
         android:textColor="#ffffff" 
         android:id="@+id/t1" /> 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="User Name :" 
         android:layout_gravity="right" 
         android:layout_marginRight="20dp" 
         android:textColor="#ffffff" 
         android:id="@+id/t2" /> 

       </LinearLayout> 



      </android.support.v7.widget.Toolbar> 
関連する問題