Tôi rất mới đối với Android và tôi dự định đăng bài này lên số Android Developers - Google Groups nhưng có vẻ như người mới cần phải đăng lên Stack Overflow. Tôi ở đây.Đưa nội dung bên dưới AppBarLayout vào CoordinatorLayout
tôi tải về phiên bản mới nhất của Android Studio 1.4.1 ngày hôm qua, và tôi đi theo các hướng dẫn trên Building Your First App. Tôi đã làm tất cả mọi thứ lên đến Starting Another Activity. Dường như các hướng dẫn này hơi cũ đối với phiên bản SDK trước, vì chúng không tham chiếu CoordinatorLayout
và AppBarLayout
mặc dù chúng xuất hiện trong mã nếu bạn làm theo các bước. Rõ ràng, tôi đã thực hiện những thay đổi nhỏ trong mã để ứng dụng này hoạt động, nhưng không hoàn toàn.
Vấn đề của tôi: Nếu bạn nhìn vào những hình ảnh ở dưới cùng của Starting Another Activity bạn sẽ thấy rằng cả hai có tiêu đề My First App. Trong sửa đổi của tôi về mã, tôi không thể có được tiêu đề này trên cả hai hình ảnh/màn hình. (Tôi nên đề cập đến mà tôi muốn sử dụng phiên bản hơn gần đây của AppBarLayout
và CoordinatorLayout
)
Hãy tập trung vào màn hình đầu tiên, activity_my.xml
là
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context=".MyActivity">
<include layout="@layout/content_my" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Như đã đề cập ở dưới cùng của Building a Simple User Interface các content_my.xml
trông giống như:
<?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:orientation="horizontal" >
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
Có anyway, tôi có thể thêm AppBarLayout
vào activity_my.xml
. Tôi đã thử một cái gì đó như:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context=".MyActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_my" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Vấn đề ở đây là các nội dung trong content_my.xml
đi đằng sau Toolbar
của AppBarLayout
chứ không phải dưới nó. Bất kỳ ý tưởng làm thế nào để khắc phục vấn đề này?
Tôi rất tiếc, bạn có thể tạo tldr không? – deadfish
Điều này có vẻ là 2 câu hỏi riêng biệt, như câu hỏi đầu tiên của bạn, để lấy tiêu đề ứng dụng trên thanh công cụ, hãy chắc chắn gọi 'setSupportActionBar (thanh công cụ) 'trong đó' thanh công cụ' là tham chiếu đến chế độ xem Thanh công cụ của bạn. Ngoài ra, lý do 'CoordinatorLayout' và' AppBarLayout' arent được tham chiếu, là bởi vì trong các phiên bản trước của Android Studio, "hoạt động trống" đã tạo ra một hoạt động hoàn toàn trống. Nó bây giờ tạo ra một bố trí đơn giản với 2 yếu tố đó. Tùy chọn mới được gọi là "Hoạt động trống". – Orbit