2015-12-31 67 views
2

this tutorial我感動我的動作條(工具欄)從我的主要佈局到另一個佈局,包括我這樣的工具欄,我的主要佈局:如何從其他佈局參考ID

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="com.marcelo.notemuevas.MainActivity"> 
<include 
    layout="@layout/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/my_toolbar"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Servicios" 
     android:id="@+id/serviciosTxt" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:background="@color/colorPrimaryDark" 
     android:textColor="#FFFFFF" 
     android:gravity="center" 
     android:paddingTop="18dp" 
     android:paddingBottom="18dp" 
     /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/serviciosTxt"> 
    </ListView> 

</RelativeLayout> 
</RelativeLayout> 

而且我的工具欄佈局這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/CustomActionBarTheme"/> 
</LinearLayout> 

但我得到了以下錯誤 enter image description here 爲什麼我不能讓我的工具欄的參考,如果有相同的ID?我可以包含我的工具欄並進行引用,以便可以將元素放置在工具欄下方?

編輯1 - 活動代碼

package com.marcelo.notemuevas; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 
    ListView list; 
    String[] itemname ={ 
      "Carpinteria" 
    }; 
    String[] descripcion={ 
      "Reparación" 
    }; 

    Integer[] imgid={ 
      R.mipmap.icono1 
    }; 
    @Override 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 
     setSupportActionBar(myToolbar); 
     myToolbar.setLogo(R.mipmap.ic_launcher); 
     CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid, descripcion); 
     list = (ListView) findViewById(R.id.list); 
     list.setAdapter(adapter); 

     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       // TODO Auto-generated method stub 
       String Slecteditem = itemname[position]; 
       Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show(); 
       Intent myIntent = new Intent(MainActivity.this, carpinteria.class); 
       myIntent.putExtra("key", 5); //Optional parameters 
       MainActivity.this.startActivity(myIntent); 
      } 
     }); 
    } 

    public boolean onCreateOptionsMenu(Menu menu){ 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     return true; 
    } 
} 

編輯2 - 解決方案

現在我讓它工作,我添加了一個新的ID給包括後來我提及它像詹姆斯·洛克哈特和潘文凱林建議(不知道爲什麼第一次沒有工作),所以我的新問題是,ID的作品像變量?只存在於已被宣佈的地方?

這是使用XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="com.marcelo.notemuevas.MainActivity"> 
<include 
    layout="@layout/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/toolbarLayout" 
    /> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/toolbarLayout"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Servicios" 
     android:id="@+id/serviciosTxt" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:background="@color/colorPrimaryDark" 
     android:textColor="#FFFFFF" 
     android:gravity="center" 
     android:paddingTop="18dp" 
     android:paddingBottom="18dp" 
     /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/serviciosTxt"> 
    </ListView> 

</RelativeLayout> 
</RelativeLayout> 
+0

糾正我,如果我錯了,但沒有資源訪問您的Toolbar一個相對的佈局需要處於相同的水平才能工作?您已將my_toolbar嵌套在使其位於一層以下的線性佈局中。雖然我可能是錯的。 你可以嘗試設置包含ID代替 –

+0

Im新的Android,我不太瞭解你想說什麼,但我從工具欄佈局中刪除了android:id =「@ + id/my_toolbar」 ,我添加的ID包括,沒有顯示錯誤,但應用程序崩潰 – Pulse9

+0

@ Pulse9,顯示您的'活動'代碼 –

回答

2

在你的XML

<include 
    android:[email protected]"+id/toolBarLayout" // set the id for layout that contains toolbar 
    layout="@layout/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/toolBarLayout"> // change my_toolbar to toolBarLayout 

然後,通過改變

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 

LinerLayout myLinear = (LinerLayout) findViewById(R.id.toolBarLayout); 
Toolbar myToolbar = (Toolbar) myLinear.findViewById(R.id.my_toolbar); 
+0

哦,是的,問題是,所以ID只能在同一個XML中工作? – Pulse9

+0

是的,id只能在同一個xml中工作 –

0

請試試這個:

  1. 在工具條佈局文件,在的LinearLayout標籤中設置了 '機器人:id' 屬性,以獨特的佈局id名稱,例如:android:id =「@ + id/toolbarLayout」

  2. 一旦你完成了(1),在你的主佈局中引用你的工具欄佈局文件如下:android:layout_below =「 @ ID/toolbarLayout」

希望這有助於。

+0

我做了第1步,但當我更改android:layout_below =「@ id/my_toolbar」> android:layout_below =「@ id/toolbarLayout」>它說toolbarLayout – Pulse9

+0

請注意,當您引用外部佈局文件時,例如layout =「@ layout/toolbar」,該佈局文件的名稱必須是toolbar.xml(並且區分大小寫...)請還要檢查R.java類文件是否已經重新生成。 R.java類文件通常是一個罪魁禍首... – reymalahay