在多个布局中使用资源 ( progressbar )
Use a resource ( progressbar ) in multiple layouts
所以我的大部分布局都具有相同的基本结构:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
style="@android:style/TextAppearance.DeviceDefault.Medium"
xmlns:autofit="http://schemas.android.com/apk/res-auto">
<ProgressBar
android:id="@+id/upperLoading"
style="?android:attr/progressBarStyle"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="20dp"
android:elevation="17dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.007"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
tools:layout_editor_absoluteX="163dp" />
<ProgressBar
android:id="@+id/mainLoading"
style="?android:attr/progressBarStyle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="142dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/dashboardScrollView"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<ScrollView
android:id="@+id/dashboardScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
//CONTENT
</ScrollView>
</android.support.constraint.ConstraintLayout>
每当我发出请求时,我都会通过更改它们在 Java 中的可见性来使用这些进度条。
如何定义 upperLoading 和 mainLoading 然后在多个布局中重用它们?我是 android 的新手,如果这个问题太简单,我很抱歉。
您可以使用可重复使用的视图创建新布局并使用 <include />
标记添加到另一个 layout.xml
查看此处了解更多信息:https://developer.android.com/training/improving-layouts/reusing-layouts.html
我设想了多种方法来解决您的问题,但我认为将您的进度条放入一个新的布局文件中,然后将其与标签一起重复使用是最好和最简单的方法。
正如documentation所说:
Inside the layout to which you want to add the re-usable component,
add the tag.
所以,在你的情况下,我会做类似的事情:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">
<include layout="@layout/upperLoading_progressbar_layout"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@android:style/TextAppearance.DeviceDefault.Medium"
xmlns:autofit="http://schemas.android.com/apk/res-auto">
...
</android.support.constraint.ConstraintLayout>
<FrameLayou>
并且根据此示例,您将需要创建一个名为 upperLoading_progressbar_layout 的新 xml 文件,您将在其中放置可重复使用的进度条。
所以我的大部分布局都具有相同的基本结构:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
style="@android:style/TextAppearance.DeviceDefault.Medium"
xmlns:autofit="http://schemas.android.com/apk/res-auto">
<ProgressBar
android:id="@+id/upperLoading"
style="?android:attr/progressBarStyle"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="20dp"
android:elevation="17dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.007"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
tools:layout_editor_absoluteX="163dp" />
<ProgressBar
android:id="@+id/mainLoading"
style="?android:attr/progressBarStyle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="142dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/dashboardScrollView"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<ScrollView
android:id="@+id/dashboardScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
//CONTENT
</ScrollView>
</android.support.constraint.ConstraintLayout>
每当我发出请求时,我都会通过更改它们在 Java 中的可见性来使用这些进度条。 如何定义 upperLoading 和 mainLoading 然后在多个布局中重用它们?我是 android 的新手,如果这个问题太简单,我很抱歉。
您可以使用可重复使用的视图创建新布局并使用 <include />
标记添加到另一个 layout.xml
查看此处了解更多信息:https://developer.android.com/training/improving-layouts/reusing-layouts.html
我设想了多种方法来解决您的问题,但我认为将您的进度条放入一个新的布局文件中,然后将其与标签一起重复使用是最好和最简单的方法。
正如documentation所说:
Inside the layout to which you want to add the re-usable component, add the tag.
所以,在你的情况下,我会做类似的事情:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">
<include layout="@layout/upperLoading_progressbar_layout"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@android:style/TextAppearance.DeviceDefault.Medium"
xmlns:autofit="http://schemas.android.com/apk/res-auto">
...
</android.support.constraint.ConstraintLayout>
<FrameLayou>
并且根据此示例,您将需要创建一个名为 upperLoading_progressbar_layout 的新 xml 文件,您将在其中放置可重复使用的进度条。