Androidとマテリアルデザイン。
このスクリーンブリーフィングでCoordinatorLayout
に関するブログを作成しようとしていました。とにかく..あなたは私の完全に機能するコードを持つことができます:
activity_scrolling.xml:
<?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="com.letsnurture.ln_202.coordinatorlayoutpart1.ScrollingActivity">
<Android.support.design.widget.AppBarLayout
Android:id="@+id/app_bar"
Android:layout_width="match_parent"
Android:layout_height="218dp"
Android:fitsSystemWindows="true"
Android:theme="@style/AppTheme.AppBarOverlay">
<Android.support.design.widget.CollapsingToolbarLayout
Android:id="@+id/toolbar_layout"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
<ImageView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:adjustViewBounds="true"
Android:src="@drawable/google_thumb"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<Android.support.v7.widget.Toolbar
Android:id="@+id/toolbar"
Android:layout_width="match_parent"
Android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</Android.support.design.widget.CollapsingToolbarLayout>
</Android.support.design.widget.AppBarLayout>
<include layout="@layout/content_scrolling" />
<Android.support.design.widget.FloatingActionButton
Android:id="@+id/fab"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_margin="@dimen/fab_margin"
Android:src="@drawable/ic_star_black_24dp"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" />
</Android.support.design.widget.CoordinatorLayout>
content_scrolling.xml:
<?xml version="1.0" encoding="utf-8"?>
<Android.support.v4.widget.NestedScrollView 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:layout_marginTop="@dimen/activity_horizontal_margin"
Android:padding="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.letsnurture.ln_202.coordinatorlayoutpart1.ScrollingActivity"
tools:showIn="@layout/activity_scrolling">
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical">
<RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/selectableItemBackground"
Android:clickable="true"
Android:padding="@dimen/activity_horizontal_margin">
<ImageView
Android:id="@+id/ivContactItem1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:layout_marginEnd="@dimen/activity_horizontal_margin"
Android:layout_marginRight="@dimen/activity_horizontal_margin"
Android:src="@drawable/ic_phone_black_24dp" />
<TextView
Android:id="@+id/tvNumber1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_toEndOf="@id/ivContactItem1"
Android:layout_toRightOf="@id/ivContactItem1"
Android:text="(123) 456-7890"
Android:textAppearance="@style/TextAppearance.AppCompat.Medium"
Android:textColor="#212121" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/tvNumber1"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_marginTop="8dp"
Android:layout_toEndOf="@id/ivContactItem1"
Android:layout_toRightOf="@id/ivContactItem1"
Android:text="Home"
Android:textAppearance="@style/TextAppearance.AppCompat.Small"
Android:textColor="#727272" />
<ToggleButton
Android:id="@+id/chkState1"
Android:layout_width="24dp"
Android:layout_height="24dp"
Android:layout_alignParentEnd="true"
Android:layout_alignParentRight="true"
Android:layout_centerVertical="true"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:background="@drawable/toggle_selector"
Android:checked="true"
Android:padding="6dp"
Android:textOff=""
Android:textOn="" />
</RelativeLayout>
<RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/selectableItemBackground"
Android:clickable="true"
Android:padding="@dimen/activity_horizontal_margin">
<ImageView
Android:id="@+id/ivContactItem2"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:layout_marginEnd="@dimen/activity_horizontal_margin"
Android:layout_marginRight="@dimen/activity_horizontal_margin"
Android:src="@drawable/ic_phone_black_24dp"
Android:visibility="invisible" />
<TextView
Android:id="@+id/tvNumber2"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_toEndOf="@id/ivContactItem2"
Android:layout_toRightOf="@id/ivContactItem2"
Android:text="(321) 654-0987"
Android:textAppearance="@style/TextAppearance.AppCompat.Medium"
Android:textColor="#212121" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/tvNumber2"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_marginTop="8dp"
Android:layout_toEndOf="@id/ivContactItem2"
Android:layout_toRightOf="@id/ivContactItem2"
Android:text="Work"
Android:textAppearance="@style/TextAppearance.AppCompat.Small"
Android:textColor="#727272" />
<ToggleButton
Android:id="@+id/chkState2"
Android:layout_width="24dp"
Android:layout_height="24dp"
Android:layout_alignParentEnd="true"
Android:layout_alignParentRight="true"
Android:layout_centerVertical="true"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:background="@drawable/toggle_selector"
Android:checked="false"
Android:padding="6dp"
Android:textOff=""
Android:textOn="" />
</RelativeLayout>
<include layout="@layout/item_divider" />
<RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/selectableItemBackground"
Android:clickable="true"
Android:padding="@dimen/activity_horizontal_margin">
<ImageView
Android:id="@+id/ivContactItem3"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:layout_marginEnd="@dimen/activity_horizontal_margin"
Android:layout_marginRight="@dimen/activity_horizontal_margin"
Android:src="@drawable/ic_email_black_24dp" />
<TextView
Android:id="@+id/tvNumber3"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_toEndOf="@id/ivContactItem3"
Android:layout_toLeftOf="@+id/chkState3"
Android:layout_toRightOf="@id/ivContactItem3"
Android:layout_toStartOf="@+id/chkState3"
Android:ellipsize="end"
Android:maxLines="1"
Android:text="[email protected]"
Android:textAppearance="@style/TextAppearance.AppCompat.Medium"
Android:textColor="#212121" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/tvNumber3"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_marginTop="8dp"
Android:layout_toEndOf="@id/ivContactItem3"
Android:layout_toRightOf="@id/ivContactItem3"
Android:text="Home"
Android:textAppearance="@style/TextAppearance.AppCompat.Small"
Android:textColor="#727272" />
<ToggleButton
Android:id="@+id/chkState3"
Android:layout_width="24dp"
Android:layout_height="24dp"
Android:layout_alignParentEnd="true"
Android:layout_alignParentRight="true"
Android:layout_centerVertical="true"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:background="@drawable/toggle_selector"
Android:checked="true"
Android:padding="6dp"
Android:textOff=""
Android:textOn="" />
</RelativeLayout>
<RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/selectableItemBackground"
Android:clickable="true"
Android:padding="@dimen/activity_horizontal_margin">
<ImageView
Android:id="@+id/ivContactItem4"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:layout_marginEnd="@dimen/activity_horizontal_margin"
Android:layout_marginRight="@dimen/activity_horizontal_margin"
Android:src="@drawable/ic_email_black_24dp"
Android:visibility="invisible" />
<TextView
Android:id="@+id/tvNumber4"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_toEndOf="@id/ivContactItem4"
Android:layout_toLeftOf="@+id/chkState4"
Android:layout_toRightOf="@id/ivContactItem4"
Android:layout_toStartOf="@+id/chkState4"
Android:ellipsize="end"
Android:maxLines="1"
Android:text="[email protected]"
Android:textAppearance="@style/TextAppearance.AppCompat.Medium"
Android:textColor="#212121" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/tvNumber4"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_marginTop="8dp"
Android:layout_toEndOf="@id/ivContactItem4"
Android:layout_toRightOf="@id/ivContactItem4"
Android:text="Work"
Android:textAppearance="@style/TextAppearance.AppCompat.Small"
Android:textColor="#727272" />
<ToggleButton
Android:id="@+id/chkState4"
Android:layout_width="24dp"
Android:layout_height="24dp"
Android:layout_alignParentEnd="true"
Android:layout_alignParentRight="true"
Android:layout_centerVertical="true"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:background="@drawable/toggle_selector"
Android:padding="6dp"
Android:textOff=""
Android:textOn="" />
</RelativeLayout>
<include layout="@layout/item_divider" />
<RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/selectableItemBackground"
Android:clickable="true"
Android:padding="@dimen/activity_horizontal_margin">
<ImageView
Android:id="@+id/ivContactItem5"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:layout_marginEnd="@dimen/activity_horizontal_margin"
Android:layout_marginRight="@dimen/activity_horizontal_margin"
Android:src="@drawable/ic_location_on_black_24dp" />
<TextView
Android:id="@+id/tvNumber5"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_toEndOf="@id/ivContactItem5"
Android:layout_toLeftOf="@+id/chkState5"
Android:layout_toRightOf="@id/ivContactItem5"
Android:layout_toStartOf="@+id/chkState5"
Android:ellipsize="end"
Android:maxLines="1"
Android:text="202 Park Street, Los Angeles"
Android:textAppearance="@style/TextAppearance.AppCompat.Medium"
Android:textColor="#212121" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/tvNumber5"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_marginTop="8dp"
Android:layout_toEndOf="@id/ivContactItem5"
Android:layout_toRightOf="@id/ivContactItem5"
Android:text="Home"
Android:textAppearance="@style/TextAppearance.AppCompat.Small"
Android:textColor="#727272" />
<ToggleButton
Android:id="@+id/chkState5"
Android:layout_width="24dp"
Android:layout_height="24dp"
Android:layout_alignParentEnd="true"
Android:layout_alignParentRight="true"
Android:layout_centerVertical="true"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:background="@drawable/toggle_selector"
Android:checked="true"
Android:padding="6dp"
Android:textOff=""
Android:textOn="" />
</RelativeLayout>
<RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="?attr/selectableItemBackground"
Android:clickable="true"
Android:padding="@dimen/activity_horizontal_margin">
<ImageView
Android:id="@+id/ivContactItem6"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:layout_marginEnd="@dimen/activity_horizontal_margin"
Android:layout_marginRight="@dimen/activity_horizontal_margin"
Android:src="@drawable/ic_email_black_24dp"
Android:visibility="invisible" />
<TextView
Android:id="@+id/tvNumber6"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_toEndOf="@id/ivContactItem6"
Android:layout_toLeftOf="@+id/chkState6"
Android:layout_toRightOf="@id/ivContactItem6"
Android:layout_toStartOf="@+id/chkState6"
Android:ellipsize="end"
Android:maxLines="1"
Android:text="221B Baker Street, England"
Android:textAppearance="@style/TextAppearance.AppCompat.Medium"
Android:textColor="#212121" />
<TextView
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_below="@id/tvNumber6"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:layout_marginTop="8dp"
Android:layout_toEndOf="@id/ivContactItem6"
Android:layout_toRightOf="@id/ivContactItem6"
Android:text="Work"
Android:textAppearance="@style/TextAppearance.AppCompat.Small"
Android:textColor="#727272" />
<ToggleButton
Android:id="@+id/chkState6"
Android:layout_width="24dp"
Android:layout_height="24dp"
Android:layout_alignParentEnd="true"
Android:layout_alignParentRight="true"
Android:layout_centerVertical="true"
Android:layout_marginLeft="@dimen/activity_horizontal_margin"
Android:layout_marginStart="@dimen/activity_horizontal_margin"
Android:background="@drawable/toggle_selector"
Android:padding="6dp"
Android:textOff=""
Android:textOn="" />
</RelativeLayout>
<include layout="@layout/item_divider" />
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical"
Android:padding="@dimen/activity_horizontal_margin">
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Send SMS" />
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginBottom="@dimen/activity_horizontal_margin"
Android:layout_marginTop="@dimen/activity_horizontal_margin"
Android:text="Call" />
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginBottom="@dimen/activity_horizontal_margin"
Android:layout_marginTop="@dimen/activity_horizontal_margin"
Android:text="Email" />
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_marginBottom="@dimen/activity_horizontal_margin"
Android:layout_marginTop="@dimen/activity_horizontal_margin"
Android:text="Remove Contact" />
</LinearLayout>
</LinearLayout>
</Android.support.v4.widget.NestedScrollView>
item_divider.xml:
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="1dp"
Android:layout_marginBottom="8dp"
Android:layout_marginTop="8dp"
Android:background="#B6B6B6" />
ScrollingActivity.Java:
public class ScrollingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
私のGithubリポジトリからソースをダウンロードできます:
Android開発者ブログは、連絡先の例のコンポーネントを紹介するためのもう1つの優れたリソースです。
http://Android-developers.blogspot.co.uk/2015/05/Android-design-support-library.html
完璧なチュートリアルはありませんが、ここから始めるのが良いでしょう。
コーディネーターレイアウト内のスクロールの処理に焦点を当てたこのチュートリアル: https://guides.codepath.com/Android/Handling-Scrolls-with-CoordinatorLayout
設計これを実現するには、以下のxmlを見てください。
<Android.support.design.widget.CoordinatorLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true">
<Android.support.design.widget.AppBarLayout
Android:id="@+id/appbar"
Android:layout_width="match_parent"
Android:layout_height="192dp"
Android:fitsSystemWindows="true"
Android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<Android.support.design.widget.CollapsingToolbarLayout
Android:id="@+id/collapsing_toolbar"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="32dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
Android:id="@+id/header"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@drawable/header"
Android:fitsSystemWindows="true"
Android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<Android.support.v7.widget.Toolbar
Android:id="@+id/anim_toolbar"
Android:layout_width="match_parent"
Android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</Android.support.design.widget.CollapsingToolbarLayout>
</Android.support.design.widget.AppBarLayout>
<Android.support.v7.widget.RecyclerView
Android:id="@+id/scrollableview"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<Android.support.design.widget.FloatingActionButton
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_margin="@dimen/fab_margin"
Android:clickable="true"
Android:src="@drawable/ic_action_add"
app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom|right|end" />
</Android.support.design.widget.CoordinatorLayout>
次に、Activity.Javaを開き、次のコードを追加します
toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
setSupportActionBar(toolbar);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("ALi Connors");
ステータスバートランスペアレントは、APIレベル21以上でサポートされています
//put below line is your style.xml.
<item name="Android:windowTranslucentStatus">true</item>
ドローアブルフォルダーにovel.xmlファイルを作成し、以下のコードをコピーアンドペーストします
//code start
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:shape="oval">
<gradient
Android:startColor="#f42b89"
Android:endColor="#f42b89"
Android:angle="270"/>
</shape>
//code end
activity_main.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"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@Android:color/white">
<!--Android:padding="8dp"-->
<RelativeLayout
Android:id="@+id/rv"
Android:layout_width="match_parent"
Android:layout_height="450dp"
Android:background="@Android:color/white">
<ImageView
Android:id="@+id/imgProfilePic"
Android:layout_width="match_parent"
Android:layout_height="390dp"
Android:adjustViewBounds="true"
Android:scaleType="fitXY"
Android:src="@drawable/images" />
<ImageView
Android:id="@+id/imgFloating"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_alignParentBottom="true"
Android:layout_alignParentRight="true"
Android:layout_marginRight="20dp"
Android:layout_marginBottom="20dp"
Android:background="@drawable/oval"
Android:elevation="5dp"
Android:padding="20dp"
Android:src="@drawable/right_icon" />
</RelativeLayout>
<ListView
Android:layout_below="@+id/rv"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@Android:color/white"></ListView>
</RelativeLayout>