【问题标题】:why my relative Layout appear as a small square instead of occupying the entire width screen?为什么我的相对布局显示为一个小方块而不是占据整个宽度屏幕?
【发布时间】:2021-06-27 17:21:54
【问题描述】:

我使用一个相对布局,其中包含一个 textView。 在运行时,布局看起来像一个小方块,而不是占据屏幕的整个宽度!?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/curriculum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/HKCurriculum" />
</RelativeLayout>

有人能告诉我我做错了什么吗?

对话框片段下方

class CvFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    var rootView: View = inflater.inflate(R.layout.fragment_cv, container, false)
    var curriculum = rootView.findViewById<View>(R.id.curriculum)

// return super.onCreateView(inflater, container, savedInstanceState)

    curriculum.setOnClickListener(object :View.OnClickListener {
        override fun onClick(v: View?) {
            dismiss()
        }
    })

    return rootView
}

}

以下是主要活动的摘录

        val hkphoto: ImageView = findViewById(R.id.HK)
    hkphoto.setOnClickListener(this)
    override fun onClick(view: View?) {
    when (view!!.id) {
        R.id.gokyo_burger -> {
            val intent = Intent(this@GokyoActivity, MainActivity::class.java)
            startActivity(intent)
        }
        R.id.HK -> {
            val cv = supportFragmentManager
            val myFragment = CvFragment()
            myFragment.show(cv, "simple Fragment")
        }

【问题讨论】:

    标签: android kotlin fragment android-relativelayout


    【解决方案1】:

    盲注:您在清单中为此Activity 设置了自定义android:configChangesattr,并且您在Activity 中的以下方法中缺少超级调用

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //super.onConfigurationChanged(newConfig); <---- !!!
    }
    

    使用完整的Activitys 代码会更容易猜到(因为屏幕上的左下角有一些青铜视图)

    【讨论】:

    • 我在清单中没有“...onConfigurationChanged...”。左下角的“青铜视图”是什么意思?我只是将屏幕隐藏在 TextView 下面的屏幕截图?
    • 我在TextView 的底线下方看到一些青铜字段,但仍在手机框架中。没有代码,一切都只是猜测。 XML 是正确的
    • 好吧,复制并粘贴到已编辑的问题中...?
    猜你喜欢
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多