【问题标题】:Android project button navigation activity fragment top marginAndroid项目按钮导航活动片段上边距
【发布时间】:2019-10-21 09:13:36
【问题描述】:

我是 android 开发人员的初学者。 此时需要删除下图所示的边距顶部。 fragment margin top

该项目有 3 个文件来展示布局。

fragment_dashboard.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"
    android:background="#153091">

    <TextView
        android:id="@+id/text_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

DashboardFragment.kt

package com.luismakeapp.myapplication.ui.dashboard

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.marginTop
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.luismakeapp.myapplication.R

class DashboardFragment : Fragment() {

    private lateinit var dashboardViewModel: DashboardViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dashboardViewModel =
            ViewModelProviders.of(this).get(DashboardViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
        val textView: TextView = root.findViewById(R.id.text_dashboard)
        dashboardViewModel.text.observe(this, Observer {
            textView.text = it
        })

        return root
    }
}

DashboardViewModel.kt

package com.luismakeapp.myapplication.ui.dashboard

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class DashboardViewModel : ViewModel() {

    private val _text = MutableLiveData<String>().apply {
        value = "This is dashboard Fragment"
    }
    val text: LiveData<String> = _text
}

哪个编辑文件可以改变布局并去除上图所示的边距?

谢谢。

【问题讨论】:

  • 你能附上你的活动布局吗?
  • dashboardViewModel.text.observe(this 应该是dashboardViewModel.text.observe(viewLifecycleOwner

标签: android-layout android-fragments kotlin viewmodel margin


【解决方案1】:

尝试从包含该片段的 Activity 的父容器布局中删除 android:paddingTop="?attr/actionBarSize"

【讨论】:

    【解决方案2】:

    您的问题有点令人困惑。试试看How do I ask a good question?

    不管怎样,你检查过你的活动吗?我假设您在 Activity 中使用 Fragment,所以我建议您检查您的 Activity 布局。

    如果您还没有这样做,那么我强烈建议您这样做。尝试在MainActivity 的布局中添加例如FrameLayout 并将其命名为fragment_container。确保不添加边距,并将 ConstraintLayout 的所有约束设置为 TopBottomStartmatch_parent结束

    之后,您可以使用菜单选项在片段之间切换,如下所示:

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    

    你可以在FragmentTransactionhere找到更多信息。

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2015-12-04
      • 2021-11-24
      相关资源
      最近更新 更多