【问题标题】:What can i make a colored background in my Fragment for my Android App?我可以在我的 Fragment 中为我的 Android 应用程序制作彩色背景吗?
【发布时间】:2020-04-27 05:50:18
【问题描述】:

我开始制作我的第一个 Android 应用程序,但我在做一些基本的事情时遇到了一些麻烦 :-) 。 我想做一个片段,它包含一个带有标题和一些长文本的屏幕。此外,背景应该是彩色的。

所以我只是为标题、滚动窗格和滚动窗格内的文本放置了一个文本表单,如下所示。 问题是,背景仅在屏幕中间着色。在顶部和末端的颜色是白色的

这是我的片段的布局:

<?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"
        android:background="@color/buttonBackground">


        <TextView
        android:id="@+id/about_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
            android:fontFamily="@font/damion"
        android:text="@string/about"
        android:textAlignment="center"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="55sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/about_text">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="80dp"
            android:text="@string/content"
            android:textColor="@color/colorPrimaryDark"
            app:layout_constraintBottom_toBottomOf="@+id/imageView"
            app:layout_constraintEnd_toEndOf="@+id/imageView"
            app:layout_constraintStart_toStartOf="@+id/imageView"
            app:layout_constraintTop_toBottomOf="@+id/about_text"
            app:layout_constraintVertical_bias="0.0" />
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    tools:context=".MainActivity"
    >

    <fragment
        android:id="@+id/myNavHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation"
        />
</LinearLayout>

屏幕如下所示: Image of the Screen

对不起初学者的问题:-)。感谢您的帮助

【问题讨论】:

  • 你的输出是什么?如果你想改变顶部屏幕的颜色,你不能在布局中改变它。
  • 现在我已经添加了输出。我应该在哪里更改顶部屏幕颜色?在activity_main.xml中?
  • @koern82 你能发布你的活动 xml 吗?
  • 我认为是因为你的图片,当你为背景设置纯色时,它会填满整个屏幕,试试android:background="#ff0000"
  • 在线性布局中将 android:layout_height="wrap_content" 改为 match-parent

标签: android android-studio android-fragments styles


【解决方案1】:

在线性布局中将android:layout_height="wrap_content"更改为android:layout_height="match_parent"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2023-04-03
    • 2016-05-02
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多