Trong ứng dụng iOS của iOS, họ đã triển khai cả nút quay lại và menu cùng nhau. Có thể thực hiện tương tự trong Android không?Có thể có cả nút quay lại và menu trong thanh công cụ Android không?
Trả lời
Bạn có thể tạo ra một thiết kế thanh công cụ tùy chỉnh trong XML như
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/backButton"
android:background="@drawable/back_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/homeButton"
android:background="@drawable/menu_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
Sau đó thiết lập thính giả để thực hiện nhiệm vụ như openDrawer và màn hình trước đó goto.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ImageView homeButton = (ImageView) toolbar.findViewById(R.id.homeButton);
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
Nếu bạn muốn một cái gì đó đặc biệt tạo ra nó bằng cách của riêng bạn :)
Nếu bạn đang sử dụng thư viện thiết kế mới nhất có thể của nó để tạo ra bảng thanh công cụ tùy chỉnh của bạn và thêm bất kỳ mục nào mà bạn muốn có.
tôi đã đạt được hành vi đó theo cách này:
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<!-- this is top bar where you need to add both drawer toggle and back button-->
<include
layout="@layout/app_bar_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Content of sliding menu-->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/primary_blue_dark"
android:fitsSystemWindows="true">
<!-- Custom menu list. Feel free to customize it as listview-->
<ListView
android:id="@+id/lv_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.NavigationView>
layout/app_bar_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:fitsSystemWindows="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_size_pre5"
android:background="@color/primary_blue_dark"
android:padding="6dp">
<!-- This is where you need to place those items. I had title and imageview there. You may add anything-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_logo"
android:adjustViewBounds="true"/>
<TextView
android:id="@+id/tv_app_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/app_name"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:textColor="@color/primary_yellow"
android:textSize="@dimen/middle_text"
android:gravity="center"/>
</LinearLayout>
<ImageView
android:id="@+id/iv_drawer_open"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_drawer_toggle"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:tint="@color/primary_white"/>
</RelativeLayout>
<!-- This is main content-->
<include layout="@layout/content_navigation" />
bạn có thể tạo một thanh công cụ với bố trí tùy chỉnh như thế này:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/backButton"
android:background="@drawable/ic_back_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/hamburgerButton"
android:background="@drawable/ic_home_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/logo"
android:background="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
Có hoàn toàn có thể bạn chỉ cần ẩn thanh công cụ mặc định bằng cách làm sau đây trong bạn Styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Sau đó, bạn phải tạo tệp xml của riêng bạn trên thanh công cụ, hãy đặt tên cho nó là custom_toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<ImageView
android:id="@+id/back_button"
android:background="@drawable/back_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/menu_button"
android:background="@drawable/menu_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:textColor="@android:color/black"
android:textSize="@dimen/actionbar_textsize"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
Sau đó, bạn cần phải thêm thanh công cụ trong hoạt động của bạn thêm thanh công cụ như dưới đây
<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="betasoft.com.blankblank.ui.ui.activity.SignUp">
<include
android:id="@+id/toolbar"
layout="@layout/custom_toolbar" />
<FrameLayout
android:id="@+id/main_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar />
</RelativeLayout>
sau đó bạn có thể truy cập vào thanh công cụ trong hoạt động của bạn như:
Toolbar mToolBar;
ImageView mToolBarBack,mToolBarMenu;
TextView mToolBarTitle;
//Inside onCreate
mToolBar = (Toolbar) findViewById(R.id.toolbar);
mToolBarBack = (ImageView) mToolBar.findViewById(R.id.toolbar_back);
mToolBarTitle = (TextView) mToolBar.findViewById(R.id.toolbar_title);
mToolBarMenu= (TextView) mToolBar.findViewById(R.id.menu_button);
// sau đó tạo ra một phương pháp nào để như bên dưới để bạn có thể truy cập nội dung trên thanh công cụ ở bất kỳ đoạn nào
public void setActionBarTitle(String title) {
mToolBarTitle.setText(title);
}
- 1. Thanh công cụ Android với cả nút home và back
- 2. Có phải 4 nút Android chuẩn không? Ví dụ nút quay lại, nút menu
- 3. Nút "đóng" trong Thanh công cụ thay vì Quay lại
- 4. chức năng nút quay lại thanh công cụ
- 5. Chuyển đổi thanh công cụ lollipop của Android giữa ngăn mở/đóng và nút quay lại
- 6. Nút quay lại Android cho hoạt động cụ thể
- 7. Hiển thị nút tràn menu trong Thanh công cụ
- 8. Nút home trên thanh công cụ không hoạt động nếu không có menu tùy chọn
- 9. Cách triển khai nút Quay lại Thanh công cụ của Android
- 10. Nút quay lại Android không quay lại hoạt động trước
- 11. Trình đơn tràn tùy chỉnh của Android (Không có thanh tác vụ và không có thanh công cụ)
- 12. Thực hiện nút quay lại "có điều kiện" trong ember.js
- 13. Hoạt động không bị đóng khi nhấp vào nút quay lại của Thanh công cụ
- 14. Tiêu đề căn chỉnh của Thanh công cụ ở giữa với nút quay lại
- 15. android webview và loadData, tôi có thể lấy nút quay lại để quay lại nội dung đã tạo không?
- 16. Điều khiển Seperator dọc trong Menu, Thanh công cụ, StackPanel, v.v. - Có thể không?
- 17. Nút quay lại Actionbarsherlock không quay lại
- 18. Cách thêm một mục menu đơn vào thanh công cụ trong android?
- 19. BlackBerry - Thanh công cụ menu tùy chỉnh
- 20. Ẩn thanh công cụ Adobe, thanh menu, thanh cuộn, v.v.
- 21. Nút quay lại Android không hoạt động
- 22. Mục menu trên Thanh công cụ không thể nhấp trên Android 4.4 (19)
- 23. Thanh công cụ có thể gập lại: Đặt số lượng thanh công cụ sẽ được thu gọn trong onCreate
- 24. iPhone: thanh điều hướng "quay lại" nút có thể nhấp = KHÔNG?
- 25. Có hợp lý để có nút "quay lại" trong ứng dụng Android không?
- 26. Thanh công cụ - thêm nút lên
- 27. Hỗ trợ Android Thanh công cụ không hiển thị tùy chỉnh có thể vẽ đúng cách
- 28. thanh công cụ android: cách tạo thanh công cụ có hình ảnh trong nền và các mục menu trên đầu trang
- 29. Không thể bắt sự kiện nút quay lại Android
- 30. Sự khác biệt giữa nút quay lại hành độngBar và nút quay lại android
thử http://stackoverflow.com/a/26525742/2032561 – Bharatesh
Vui lòng đăng ảnh và/hoặc XML lay hiển thị * chính xác * toàn bộ thanh công cụ của bạn cần trông như thế nào khi hoàn tất. Điều này quan trọng đối với giải pháp. –
nó nên tương tự như ứng dụng amazon ios. tôi đã đăng ảnh chụp màn hình xin vui lòng kiểm tra xem nó –