【问题标题】:How to layout textview to make it be center in the 1/4 position of the parent linearLayout?如何布局 textview 使其居中于父 linearLayout 的 1/4 位置?
【发布时间】:2013-06-04 09:22:10
【问题描述】:

要清楚,如果父linearLayout是480dp,我需要我的textview居中在80 dp的位置,如果textview是10 dp宽度,那么它需要75dp到85 dp作为它的空间,我该怎么做?

【问题讨论】:

    标签: android layout textview android-linearlayout


    【解决方案1】:

    我相信,您可以使用对象的 layout weight 属性来做到这一点。例如:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/background">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="whatever" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@color/background" />
    
    </LinearLayout>
    

    在本例中,View 对象用于填充空白空间并强制使用 TextView 对象的 1/4 权重。我对我的 Android 编程有点生疏,所以我希望这对你有用。

    【讨论】:

    • 不,这不行,我的意思是 textview 应该居中在 80 dp 的位置,这意味着它可能需要 75 到 85 作为它的空间。
    • 啊,我明白了。我的错。我暂时将其留在这里,以免其他人给出相同的答案。
    【解决方案2】:

    您可以使用layout_weightTextView 赋予父级宽度的一半,然后使用android:gravity 将文本居中。

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" 
            android:gravity="center" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-16
      • 2015-10-14
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多