已更新 我采用了@theThapa发布的似乎有效的示例布局,并对其进行了两项编辑:我取出了android:tint线,并用单个矢量drawable替换了布局中的三个drawable:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/media_audio_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<ImageView
android:id="@+id/topImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/linked_hands_transluscent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="ContentDescription"
/>
<LinearLayout
android:id="@+id/actionButtonsContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/topImageView"
>
<ImageButton
android:id="@+id/btnRecord"
android:layout_width="50dp"
android:layout_height="50dp"
tools:src="@drawable/ic_file_download_black_24dp"
tools:ignore="ContentDescription"
/>
<ImageButton
android:id="@+id/btnPlay"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_file_download_black_24dp"
/>
<ImageButton
android:id="@+id/btnStop"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_file_download_black_24dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/deleteSaveCancelContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvMessage"
>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="50dp"
tools:text="delete"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginStart="8dp"
tools:text="save"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginStart="8dp"
tools:text="cancel"
/>
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="start|center_vertical"
app:layout_constraintTop_toBottomOf="@+id/actionButtonsContainer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Press the Microphone to record your description. Press the Play and Stop buttons to review your recording"/>
我没有看到正确的结果,如下面作为答案发布的结果。我真的很想知道区别是否在玩的Android Studio和/或ConstraintLayout版本中(我使用的是最新的ConstraintLayout-2.0.0-alpha5)
已更新 我发现如果我将图像“预加载”到ImageView中,然后将图像换成我想要的真实图像,那么布局就可以正确完成了。我仍然认为这是一种我不需要做的变通办法,但至少我已经使那部分工作了。
现在我的帖子的第二部分-横向位置布局不正确仍然是一个问题。为什么会这样呢?
原始帖子 我正在使用ConstraintLayout设计一个屏幕,该屏幕显示AppCompatImageView,分隔线,一些用于记录/播放音频的按钮以及一些用于删除/保存/取消的按钮。在Android Studio中,预览看起来不错:
但是在运行时,我什么都看不到。用于将图像加载到ImageView中的loadImageIntoImageView()方法在与此相同图像的其他上下文中正常工作,因此我认为这与ConstraintLayout有关。我已经这样输入日志了:
AppCompatImageView view = findViewById(R.id.media_image_view);
InTouchUtils.loadImageIntoImageView(pathToMedia, this, view);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Make sure the new entry is visible
Timber.d("Lifecycle: ImageView size is: w:%d, h:%d", view.getWidth(), view.getHeight());
}
});
看来我在(Samsung S7 Edge)上正在测试的设备被赋予ImageView的全宽(1440像素),但高度只有1像素:
我不明白的另一件事是,当我在Android Studio中将方向更改为横向时,预览结果完全不是我期望的(而且也不是设备上实际发生的事情-我得到的结果是相同的如纵向):
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/media_audio_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/media_image_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/grandparents_grandchildren_transluscent"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintBottom_toTopOf="@+id/annotation_controls_separator"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="all" />
<View
android:id="@+id/annotation_controls_separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="?android:attr/dividerVertical"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintBottom_toTopOf="@+id/progress_recording_layout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/media_image_view" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/progress_recording_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintBottom_toTopOf="@+id/media_buttons_layout"
app:layout_constraintEnd_toEndOf="@+id/media_buttons_layout"
app:layout_constraintStart_toStartOf="@+id/media_buttons_layout"
app:layout_constraintTop_toBottomOf="@+id/annotation_controls_separator">
<ProgressBar
android:id="@+id/audioProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/recordingLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/text_margin_small"
android:paddingEnd="@dimen/text_margin_small"
android:text="@string/media_recording_label"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="@color/colorAccent"
android:visibility="gone" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/media_buttons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintBottom_toTopOf="@+id/media_buttons_explanation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progress_recording_layout">
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/media_record_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/description_media_record_start"
android:src="@drawable/ic_mic_black_24dp" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/media_play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/description_media_record_play"
android:enabled="false"
android:src="@drawable/ic_play_arrow_black_24dp" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/media_stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/description_media_record_stop"
android:enabled="false"
android:src="@drawable/ic_stop_black_24dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/media_buttons_explanation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="@dimen/text_margin_small"
android:paddingEnd="@dimen/text_margin_small"
android:text="@string/media_record_explanation_label"
android:textAppearance="@android:style/TextAppearance.Material.Small"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintBottom_toTopOf="@+id/horizontal_view1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/media_buttons_layout" />
<View
android:id="@+id/horizontal_view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="?android:attr/dividerVertical"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintBottom_toTopOf="@+id/dismissButtons"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/media_buttons_explanation" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/dismissButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintTop_toBottomOf="@+id/horizontal_view1"
app:layout_constraintBottom_toTopOf="@+id/horizontal_view2"
style="?android:attr/buttonBarStyle"
android:gravity="top|center"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/recording_delete_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="@string/media_delete_label" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/recording_save_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="@string/media_save_label" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/recording_cancel_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/media_cancel_label" />
</androidx.appcompat.widget.LinearLayoutCompat>
<View
android:id="@+id/horizontal_view2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintVertical_weight="1"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dismissButtons" />
</androidx.constraintlayout.widget.ConstraintLayout>
我觉得我肯定在做某些明显错误的事情,但我看不到。
答案 0 :(得分:0)
@tfrysinger,请参阅以下xml
。我也附上了屏幕截图。格式化不是最好的,请不要介意。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/media_audio_root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:id="@+id/topImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/ic_file"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="ContentDescription"
/>
<LinearLayout
android:id="@+id/actionButtonsContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/topImageView"
>
<ImageButton
android:id="@+id/btnRecord"
android:layout_width="50dp"
android:layout_height="50dp"
android:tint="@color/gray_dark"
tools:src="@drawable/ic_file"
tools:ignore="ContentDescription"
/>
<ImageButton
android:id="@+id/btnPlay"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:tint="@color/gray_dark"
tools:ignore="ContentDescription"
tools:src="@drawable/settings_icon"
/>
<ImageButton
android:id="@+id/btnStop"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:tint="@color/gray_dark"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_sync"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/deleteSaveCancelContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvMessage"
>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:tint="@color/gray_dark"
tools:text="delete"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:tint="@color/gray_dark"
tools:text="save"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:tint="@color/gray_dark"
tools:text="cancel"
/>
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="start|center_vertical"
app:layout_constraintTop_toBottomOf="@+id/actionButtonsContainer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Press the Microphone to record your description. Press the Play and Stop buttons to review your recording"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>