TextView中的水平居中文本不起作用

时间:2018-08-12 20:18:34

标签: android xml android-layout android-studio

这听起来可能很愚蠢,但是我为此感到挣扎,所以请耐心等待几秒钟。

因此,我想将一个简单的TextView放在屏幕中央:

<TextView
    android:id="@+id/helloTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:includeFontPadding="false"
    android:text="Hello!"
    android:textSize="50sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.464" />

我使用wrap_contentmatch_constraint进行了尝试,但无法使它居中。当我执行wrap_content时,框会正确居中,但其中的文本却无法如您在此处看到:

1st blueprint

当我做match_constraint时,它只是垂直居中,而不是水平居中:

2nd blueprint

我错过了什么?

2 个答案:

答案 0 :(得分:2)

在您发布的TextView的xml中添加:

function observe(obj) {

    Object.keys(obj).forEach(key => {
      let internalValue = obj[key]
      let dep = new Dep()
      Object.defineProperty(obj, key, {
        get() {
          dep.depend()
          return internalValue
        },
        set(newValue) {
          const isChanged = internalValue !== newValue
          if (isChanged) {
            internalValue = newValue
            dep.notify()
          }
        }
      })
    })
  }

  class Dep {
    constructor() {
      this.subscribers = new Set()
    }

    depend() {
      if (activeUpdate) {
        // register the current active update as a subscriber
        this.subscribers.add(activeUpdate)
      }
    }

    notify() {
      // run all subscriber functions
      this.subscribers.forEach(subscriber => subscriber())
    }
  }

  let activeUpdate

  function autorun(update) {
    function wrappedUpdate() {
      activeUpdate = wrappedUpdate
      update()
      activeUpdate = null
    }
    wrappedUpdate()
  }


  const state = {
    count: 0
  }

  observe(state)

  state.count;

  autorun(() => {
    console.log(`count is: ${state.count}`)
  })

  state.count++

答案 1 :(得分:0)

<div id="map" style="position:absolute; width:100%; height:100%; background:grey;z-index:0">
    <div style="position:absolute;">
        <div class="forbut">
            <button onclick="myFunction()" class="btn-danger">edf</button>
        </div>
        <div id="list" style=" width:100px; left:0;  background-color: red; z-index:1">

        </div>
        <div style="position:absolute; left:600px;top:0">
            <div class="forbut1">
                <button onclick="myFunction1()" class="btn-danger">edf</button>
            </div>
            <div id="list1" style=" width:100px; left:600px;  background-color: red; z-index:1">

            </div>
        </div>


    </div>
</div>