【问题标题】:Android Studio - Fill remaining blank space (with height?)Android Studio - 填充剩余的空白空间(高度?)
【发布时间】:2018-05-03 19:00:34
【问题描述】:

我有这个:Application

我想抓取较浅的蓝色文本 (code) 并使其自动填充空白空间(如果有),例如:example(可以是空白文本)。

要调整大小的文本代码(第二个 TextView,除非您可以调整布局大小):

    <!-- Info -->
    <LinearLayout
        android:id="@+id/personalAppLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/backgroundColor">

        <TextView
            android:id="@+id/descriptionTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/homepage_title"
            android:textAlignment="center"
            android:textStyle="bold"
            android:layout_marginTop="@dimen/activity_vertical_margin"/>

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/homepage_description"
            android:textAlignment="center"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:layout_marginBottom="@dimen/activity_vertical_margin"/>

    </LinearLayout>

    <!-- Greyish Line -->
    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="@color/backgroundColorDark" />

这是图片的代码:Images

    <!-- Info -->
    <LinearLayout
        android:id="@+id/personalAppLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/backgroundColor">

        <TextView
            android:id="@+id/descriptionTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/homepage_title"
            android:textAlignment="center"
            android:textStyle="bold"
            android:layout_marginTop="@dimen/activity_vertical_margin"/>

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/homepage_description"
            android:textAlignment="center"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:layout_marginBottom="@dimen/activity_vertical_margin"/>

    </LinearLayout>

底部导航码:Bottom Navigation

    <!-- Bottom Navigation -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation"
            android:layout_alignParentBottom="true"/>

    </RelativeLayout>

一切都在垂直方向的 LinearLayout 中,在这些布局之前还有更多布局,但我相信这些才是最重要的。

是否可以在不指定dp数量的情况下自动填充手机上的空白?

作为一个整体的代码(我打算自动调整第一个 LinearLayout 的大小,直到 所有剩余的空白空间都被填充,不重叠任何 其他布局)

编辑:感谢 Rockney,现在已将其更改为完整的工作代码

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">


<!-- Info -->
<LinearLayout
    android:id="@+id/personalAppLayout"
    android:layout_width="match_parent"
    <!-- Previously android:layout_height="wrap_content" and without a weight -->
    android:layout_height="0dp"
    android:weight="1"
    android:orientation="vertical"
    android:background="@color/backgroundColor">

    <TextView
        android:id="@+id/descriptionTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/homepage_title"
        android:textAlignment="center"
        android:textStyle="bold"
        android:layout_marginTop="@dimen/activity_vertical_margin"/>

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/homepage_description"
        android:textAlignment="center"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"/>

</LinearLayout>

<!-- Greyish Line -->
<View
    android:layout_width="match_parent"
    android:layout_height="0.1dp"
    android:background="@color/backgroundColorDark" />

<!-- Social Media -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:background="@color/backgroundColor"
    android:gravity="center" >

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="@drawable/linkedin_logo" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="@drawable/facebook_logo" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="@drawable/twitter_logo" />

</LinearLayout>

<!-- Greyish Line -->
<View
    android:layout_width="match_parent"
    android:layout_height="0.1dp"
    android:background="@color/backgroundColorDark" />

<!-- Bottom Navigation -->
<RelativeLayout
    android:layout_width="match_parent"
    <!-- Previously android:layout_height="match_parent" -->
    android:layout_height="60dp" >

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

</LinearLayout>

【问题讨论】:

  • 在哪里输入有偏好吗?像pastebin?还是我在这里输入?
  • 我已经将代码粘贴为文本,抱歉。

标签: android height space fill


【解决方案1】:

您可以使用布局权重。它指定有多少额外空间用于视图。 you can read more about in this stackoverflow post. 你需要把你的布局改成这个。

<LinearLayout
android:id="@+id/personalAppLayout"
android:layout_width="match_parent"
android:layout_height="0dp”
android:orientation="vertical"
android:layout_weight=“1”
android:background="@color/backgroundColor">

<TextView
    android:id="@+id/descriptionTitle"
    android:layout_width="match_parent"
    android:layout_weight=“1”
    android:layout_height="0dp"
    android:text="@string/homepage_title"
    android:textAlignment="center"
    android:textStyle="bold"
    android:layout_marginTop="@dimen/activity_vertical_margin"/>

<TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/homepage_description"
    android:textAlignment="center"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_marginBottom="@dimen/activity_vertical_margin"/>

 </LinearLayout>

【讨论】:

  • 就是这样,谢谢。我还添加了将 RelativeLayout 高度从“match_parent”编辑为固定大小(例如 60dp)以使其正常工作。
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
  • 2010-09-10
相关资源
最近更新 更多