首先,我是Android开发的新手。
我的问题是关于Android Studio / Development中不同视图的对齐。
特别是图标和文本的正确高度对齐。
如您所见,我尝试将文本与图标对齐。但是,结果在模拟器中看起来略有不同。
这是我的.xml文件:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="352dp"
android:fontFamily="@font/poppins_medium"
android:text="Dashboard"
android:textAlignment="viewStart"
android:textColor="@color/colorText"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<ImageView
android:id="@+id/imageView10"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3"
app:srcCompat="@drawable/ic_settings_black_24dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的问题是:
1。。如何解决这两个视图的对齐问题?
2。。模拟器中的结果为什么不同?
3。。有没有最佳方法来正确对齐视图?
答案 0 :(得分:0)
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="352dp"
android:fontFamily="@font/poppins_medium"
android:text="Dashboard"
android:textAlignment="viewStart"
android:textColor="@color/colorText"
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView10" />
<ImageView
android:id="@id/imageView10"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="32dp"
app:srcCompat="@drawable/ic_settings_black_24dp"
app:layout_constraintTop_toTopOf="@id/textView3"
app:layout_constraintBottom_toBottomOf="@id/textView3"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 1 :(得分:0)
您只需使用 自定义工具栏 并向其中添加菜单即可。
答案 2 :(得分:0)
尝试使用Custom toolbar
:
<LinearLayout 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:orientation="vertical"
tools:context=".terminalShortActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#4EACE2"
app:popupTheme="@style/ThemeOverlay.AppCompat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terminal Shortcuts"
android:textColor="#FFFFFF" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
答案 3 :(得分:0)
因此,我进一步研究了对齐问题,并提出了一些“解决方案”。一般来说,要回答我的问题,“自定义工具栏”不是对齐视图的正确解决方案。
问题在于字体的填充。 Android会采用整个视图来对齐视图,而不仅仅是假设“文本”。因此,对齐方式看起来不正确。
可以使用以下代码消除底部填充:
android:includeFontPadding="false"
或检查此线程以更深入地了解issues of font-padding。
要对齐视图,现在我使用 Chain-Constraints (链约束),并且不进行填充。看起来是对齐视图(尤其是文本视图)的最佳方法。但是,我不得不说,尽管删除了底部填充,但仍需要使用垂直偏置稍微调整对齐方式。
供参考: