2015-10-09 92 views
3

爲什麼toolbar未顯示在底部?工具欄未顯示在屏幕底部

private void initToolbars() { 

    Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top); 
    setSupportActionBar(toolbarTop); 

    Toolbar toolbarBottom = (Toolbar) findViewById(R.id.drawer_layout); 
    toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { 
     @Override 
     public boolean onMenuItemClick(MenuItem item) { 
      switch(item.getItemId()){ 
       case R.id.action_settings: 
        // TODO 
        break; 
       // TODO: Other cases 
      } 
      return true; 
     } 
    }); 
    toolbarBottom.inflateMenu(R.menu.menu_main); 

} 

這個XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_top" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" /> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/drawer_layout" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_gravity="bottom" 
     android:minHeight="?attr/actionBarSize" /> 



</RelativeLayout> 

我得到的工具欄在頂部只有

+0

檢查此答案http://stackoverflow.com/questions/27482012/how-to-show-action-items-at-the-bottom-using-toolbar –

+1

我從該鏈接的代碼,Layout_height有wrap_content也是。你建議改變它來匹配父母? – Moudiz

回答

2

嘗試這種方式

<RelativeLayout ...> 
    <!-- top toolbar --> 
    <Toolbar 
     android:id="@+id/top_toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="?attr/colorPrimary" 
    > 

     ... 

    </Toolbar> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/top_toolbar" 
     android:src="@drawable/week1" 
    /> 

    <!-- bottom toolbar --> 
    <Toolbar 
     android:id="@+id/bottom_toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#000000" 
     android:layout_alignParentBottom="true"> 

    </Toolbar> 

    <!-- scrollable pane --> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/image" 
     android:layout_above="@id/bottom_toolbar"> 

     ... 

    </ScrollView> 
</RelativeLayout> 
+0

好嗎生病測試 – Moudiz

+0

@Moudiz是否有效?是否有錯誤? –

+0

它的工作。這就是爲什麼Accepted.Happy編碼。 –

2

當你已經定義android:layout_alignParentBottom="true"則無需添加android:layout_gravity="bottom"

對於更多的問題,你可以訪問here.I希望它會幫助你。

How to show action items at the bottom using Toolbar

+1

即使刪除android:layout_gravity =「bottom」其仍然沒有顯示,並且我從之前提到的鏈接代碼中提到了 – Moudiz

+1

HAppycoding:)..... –

+0

@Lakhan(Y)向前移動 –

2

嘗試以下,這是爲我工作

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1" 
> 
<!--Begin Top Bar--> 
<android.support.v7.widget.Toolbar 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" /> 
</LinearLayout> 

<!--Bottom bar--> 
<android.support.v7.widget.Toolbar 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:alignParentBottom="true"/></LinearLayout> 
2

可能的問題是總重量的工具欄。

嘗試使用下面的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 


<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:layout_alignParentBottom="true" 
    android:background="?attr/colorPrimary" /> 

你MainActivity.java

public class MainActivityextends AppCompatActivity { 

private Toolbar toolbar; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

希望它可以幫助你。

+0

當我應用你的代碼我有thi錯誤..你能知道爲什麼... http://stackoverflow.com/questions/33045449/this-activity-already-has-an-action-酒吧提供的,由這窗口裝飾-DO-不-REQ – Moudiz

相關問題