Android工具栏更改布局
我有一个有很多片段的活动。当我更改片段时,我还需要更改,并且我的工具栏在MainActivity.class中声明。我的问题是,我包含了一个布局,但没有找到一种方法来更改此布局。在这种情况下,如何更改工具栏布局? MainActivity.classAndroid工具栏更改布局,android,android-toolbar,Android,Android Toolbar,我有一个有很多片段的活动。当我更改片段时,我还需要更改,并且我的工具栏在MainActivity.class中声明。我的问题是,我包含了一个布局,但没有找到一种方法来更改此布局。在这种情况下,如何更改工具栏布局? MainActivity.class @ContentView(R.layout.activity_main) public class MainActivity extends RoboActionBarActivity { @InjectView(R.i
@ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
@InjectView(R.id.tool_bar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
String currentFragmentClass = fragment.getClass().getSimpleName();
if (currentFragmentClass.equals(getString(R.string.info_fragment))) {
//here i need to change and set onclicks
}
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".activities.MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
<View
android:id="@+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorToolBar"
>
<RelativeLayout
android:id="@+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="@mipmap/icon_sliding_menu" />
<ImageView
android:id="@+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/jammboree" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/app_logo"
android:padding="5dp">
<ImageView
android:id="@+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="@mipmap/icon_my_cart" />
<TextView
android:id="@+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/hotlist_bell"
android:layout_alignTop="@id/hotlist_bell"
android:background="@drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="@+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".activities.MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
<View
android:id="@+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorToolBar"
>
<RelativeLayout
android:id="@+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="@mipmap/icon_sliding_menu" />
<ImageView
android:id="@+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/jammboree" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/app_logo"
android:padding="5dp">
<ImageView
android:id="@+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="@mipmap/icon_my_cart" />
<TextView
android:id="@+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/hotlist_bell"
android:layout_alignTop="@id/hotlist_bell"
android:background="@drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="@+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
如果我理解正确,您希望在更改用户当前看到的片段时更改工具栏,对吗 有一种非常简单的方法可以通过XML实现这一点。假设您需要三个不同样式的工具栏 1) 首先,为需要的每个工具栏创建XML文件 2) 然后将每个工具栏都包含在活动\u main.xml中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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=".activities.MainActivity">
<include
android:id="@+id/toolbar1"
layout="@layout/tool_bar1"
android:visibility="visible" />
<include
android:id="@+id/toolbar2"
layout="@layout/tool_bar2"
android:visibility="gone" />
<include
android:id="@+id/toolbar3"
layout="@layout/tool_bar3"
android:visibility="gone" />
// Here is the rest of your XML code
</RelativeLayout>
根据你的需要,改变你要让哪一个可见
希望这有帮助。我认为@herrmartell的方法不好 按照我的方式,我在一个活动中有三个片段,我为一个通用的工具栏xml定义了一些不同的布局资源,并在单击不同的按钮时将它们加载到工具栏中;我的代码:
<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<!-- your custom layout -->
</android.support.v7.widget.Toolbar>
public void switchToolbar(int layout) {
if(currentToolbarLayout == layout){
return;
}
currentToolbarLayout = layout;
View v = getLayoutInflater().inflate(layout,null);
toolbar.removeAllViews();
toolbar.addView(v);
switch (layout){
case R.layout.activity_main_toolbar_common_view:
v.findViewById(R.id.action_location).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到位置", Toast.LENGTH_SHORT).show();
}
});
break;
case R.layout.activity_main_toolbar_mine_view:
v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到设置", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
ButterKnife.bind(this);
setSupportActionBar(toolbar);
switchToolbar(R.layout.activity_main_toolbar_common_view);
switchToolbar(R.layout.activity_main_toolbar_mine_view);
我对一个类似问题的回答被否决了。它有帮助吗?这很好,但是需要更多的精力来编写和维护,并且不能真正提供性能优势。此外,您的解决方案实际上并没有解决更改工具栏属性本身的问题,即背景和前景颜色、高度等。请记住,您应该始终尝试保持代码尽可能简单和干净。我更喜欢tbh;随着时间的推移,
main活动中的片段将发生变化。我看不到将所有特定工具栏放在main活动中的可维护性。特别是当自定义工具栏比较复杂时。添加到@herrmartell注释。很容易将视图的最后一个实例保留在工具栏中,而不会产生太多开销。删除所有视图然后附加视图会丢失视图的实例,需要重新创建它。因此,对于我的用例场景,我选择hermartell。
switchToolbar(R.layout.activity_main_toolbar_mine_view);