我一直在尝试实现类似于的功能,请注意图像在状态栏后面的位置。问题是我无法正常工作。
我已经完成的工作看起来像这样:我已经完成了,您可能会问?好吧,这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:attrs="http://schemas.android.com/apk/res-auto"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CarDetailActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
tools:context=".CarDetailActivity">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_bg"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="450dp">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="450dp"
android:background="@color/colorPrimary"
android:clipToPadding="false"
android:fitsSystemWindows="true" />
<com.rd.PageIndicatorView
android:id="@+id/pageIndicatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="@+id/viewPager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:piv_animationType="scale"
app:piv_autoVisibility="true"
app:piv_dynamicCount="true"
app:piv_interactiveAnimation="true"
app:piv_selectedColor="@android:color/white"
app:piv_unselectedColor="@android:color/darker_gray"
app:piv_viewPager="@id/viewPager"
attrs:piv_padding="8dp"
attrs:piv_radius="4dp" />
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/transparent"
app:elevation="0dp"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/car_detail_name"
android:textSize="24sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:baselineAligned="false"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewPager">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:src="@drawable/ic_model_year" />
<me.grantland.widget.AutofitTextView
android:id="@+id/model_year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold"
autofit:minTextSize="14sp"
tools:text="2015" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/model_year"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:src="@drawable/ic_fuel_type" />
<me.grantland.widget.AutofitTextView
android:id="@+id/fuel_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold"
tools:text="Petrol" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/fuel_type"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/row2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/row1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:src="@drawable/ic_vehicle_count" />
<me.grantland.widget.AutofitTextView
android:id="@+id/vehicle_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold"
tools:text="2015" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/vehicle_count"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:src="@drawable/ic_location" />
<me.grantland.widget.AutofitTextView
android:id="@+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold"
tools:text="Petrol" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/location"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp">
<TextView
android:id="@+id/more_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="@string/more_details"
android:textColor="@android:color/black"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/detail1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/exterior_color"
android:textColor="@android:color/darker_gray"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/more_details" />
<TextView
android:id="@+id/detail2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/interior_color"
android:textColor="@android:color/darker_gray"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail1" />
<TextView
android:id="@+id/detail3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/vin"
android:textColor="@android:color/darker_gray"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail2" />
<me.grantland.widget.AutofitTextView
android:id="@+id/exterior_color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:maxLines="1"
android:singleLine="true"
android:text="deggdsgfsd"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/detail1"
app:layout_constraintTop_toBottomOf="@id/more_details" />
<me.grantland.widget.AutofitTextView
android:id="@+id/interior_color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/detail1"
app:layout_constraintTop_toBottomOf="@+id/exterior_color" />
<me.grantland.widget.AutofitTextView
android:id="@+id/vin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/detail1"
app:layout_constraintTop_toBottomOf="@+id/interior_color" />
<View
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="1px"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/darker_gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vin" />
<TextView
android:id="@+id/detail4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="@string/specification"
android:textColor="@android:color/black"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider" />
<TextView
android:id="@+id/specification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/specification"
android:textColor="@android:color/darker_gray"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>
这也是我的活动代码:
override fun onCreate(savedInstanceState: Bundle?) {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
super.onCreate(savedInstanceState)
setContentView(R.layout.car_detail_activity)
viewModel = ViewModelProviders.of(this).get(CarDetailViewModel::class.java)
viewModel.getCarDetail(intent.getParcelableExtra("user"),
intent.getIntExtra("id", 0))
viewModel.carDetail.observe(this, Observer {
loadData(it)
})
}
这是我在应用程序中使用的主题:
<style name="AppTheme.Detail">
<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
<item name="android:windowEnableSplitTouch">false</item>
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>
</style>
对于viewPager,这是适配器:
class ImagesPagerAdapter(private val context: Context, private val images: List<String>) : PagerAdapter() {
override fun instantiateItem(collection: ViewGroup, position: Int): Any {
val layout = LayoutInflater.from(context).inflate(R.layout.item_detail_pager, collection, false) as ViewGroup
GlideApp
.with(context)
.load(images[position])
.error(R.drawable.car_placeholder)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
layout.pagerImage.invalidate()
return true
}
})
.centerCrop()
.into(layout.pagerImage)
collection.addView(layout)
return layout
}
override fun destroyItem(container: ViewGroup, position: Int, view: Any) {
container.removeView(view as View)
}
override fun getCount(): Int {
return this.images.size
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}
}
这是图像的布局
<?xml version="1.0" encoding="utf-8"?>
<ImageView android:id="@+id/pagerImage"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:src="@drawable/ic_background_login"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
问题是我可以在状态栏下滚动,但是当imageview在顶部留出一些空间