【问题标题】:Center text in TextView and Button在 TextView 和 Button 中居中文本
【发布时间】:2014-09-09 20:31:54
【问题描述】:

我正在尝试将 TextViewButton 中的文本居中。我目前有以下代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"        
        android:orientation="vertical"
        android:background="#000000" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_weight="0.15"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="@string/CouponFinder"
            android:textSize="30sp"
            android:textColor="#FF0000"
            android:typeface="monospace" />

        <ImageView
            android:id="@+id/couponImage"
            android:layout_width="match_parent"
            android:layout_weight="0.7"
            android:layout_height="0dp"
            android:src="@drawable/coupon" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_weight="0.15"
            android:layout_height="0dp"
            android:text="@string/continueButton"
            android:textSize="30sp" 
            android:gravity="center"/>

    </LinearLayout>

</RelativeLayout>

但是,ButtonTextView 中的文本位于右侧,因此某些文本不在屏幕上...我的 LinearLayout 也收到以下警告:

This LinearLayout layout or its RelativeLayout parent is possibly useless

任何帮助将不胜感激!

【问题讨论】:

  • 意识到下面的一些建议确实有效......当我在手机上测试时,输出是正确的。然而,由于某种原因,Eclipse 中的图形布局不能正确显示。感谢大家的帮助!

标签: android layout textview


【解决方案1】:

试试这个

[<TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="0.15"
                android:gravity="center"
                android:hint="asdasasdasdd"
                android:text="adasdasdasd"
                android:textAlignment="gravity"
                android:textColor="#FFffff"
                android:textSize="30sp"
                android:typeface="monospace" />]

【讨论】:

    【解决方案2】:

    试试这个,

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    
    tools:context=".MainActivity" >
    
          <ImageView
                android:id="@+id/couponImage"
                android:layout_width="wrap_content"
                android:layout_weight="0.7"
                android:layout_height="wrap_content"
                android:src="@drawable/coupon" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:gravity="center"
            android:text="continueButton"
            android:textSize="30sp" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:gravity="center_horizontal"
            android:text="CouponFinder"
            android:textColor="#FF0000"
            android:textSize="30sp"
            android:typeface="monospace" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      试试这个..

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
              android:orientation="vertical"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity"
              android:background="#000000" >
      
              <TextView
                  android:id="@+id/textView1"
                  android:layout_width="match_parent"
                  android:layout_weight="0.15"
                  android:layout_height="0dp"
                  android:gravity="center"
                  android:text="CouponFinder"
                  android:textSize="30sp"
                  android:textColor="#FF0000"
                  android:typeface="monospace" />
      
              <ImageView
                  android:id="@+id/couponImage"
                  android:layout_width="match_parent"
                  android:layout_weight="0.7"
                  android:layout_height="0dp"
                  android:src="@drawable/icon" />
      
              <Button
                  android:id="@+id/button1"
                  android:layout_width="match_parent"
                  android:layout_weight="0.15"
                  android:layout_height="0dp"
                  android:text="continueButton"
                  android:textSize="30sp" 
                  android:gravity="center"/>
      
          </LinearLayout>
      

      【讨论】:

      【解决方案4】:
        1234563 .
      1. 尝试在项目上设置layout-gravity,而不是gravity

      希望这会有所帮助:)

      【讨论】:

      • 感谢 Gil,但我仍然没有得到正确的对齐方式。这是我做出您建议的更改后的当前样子。 i.imgur.com/uvbSFjd.png
      【解决方案5】:

      试试这个方法:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/linearLayout1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#000000"
      android:orientation="vertical" >
      
      <TextView
          android:id="@+id/textView1"
          android:layout_width="fill_parent"
          android:layout_height="0dp"
          android:layout_weight="0.15"
          android:gravity="center"
          android:text="CouponFinder"
          android:textColor="#FF0000"
          android:textSize="30sp"
          android:typeface="monospace" />
      
      <ImageView
          android:id="@+id/couponImage"
          android:layout_width="fill_parent"
          android:layout_height="0dp"
          android:layout_weight="0.7"
          android:src="@drawable/icon" />
      
      <Button
          android:id="@+id/button1"
          android:layout_width="fill_parent"
          android:layout_height="0dp"
          android:layout_weight="0.15"
          android:gravity="center"
          android:text="continueButton"
          android:textSize="30sp" />
      
        </LinearLayout>
      

      【讨论】:

      • 嗨,tsp,使用此代码,我得到的输出与我最初的输出相同 =/ i.imgur.com/WsKKhZZ.png
      【解决方案6】:

      警告是因为您只使用一个LinearLayout 作为RelativeLayout 的子视图。因此请删除您的RelativeLayout

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/linearLayout1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"        
          android:orientation="vertical"
          android:background="#000000" >
      
           <TextView
          android:id="@+id/textView1"
          android:layout_width="match_parent"
          android:layout_weight="0.15"
          android:layout_height="0dp"
          android:gravity="center"
          android:text="@string/CouponFinder"
          android:textSize="30sp"
          android:textColor="#FF0000"
          android:typeface="monospace" />
      
          <ImageView
              android:id="@+id/couponImage"
              android:layout_width="match_parent"
              android:layout_weight="0.7"
              android:layout_height="0dp"
              android:text="@string/CouponFinder" />
      
          <Button
              android:id="@+id/button1"
              android:layout_width="match_parent"
              android:layout_weight="0.15"
              android:layout_height="0dp"
               android:text="@string/continueButton"
              android:textSize="30sp" 
              android:gravity="center"/>
      
      </LinearLayout>
      

      试试上面的数据。我通过上面的数据得到了这个

      【讨论】:

      • 感谢 Manishika,但这会将文本视图中的文本对齐到左上角并且没有更改按钮文本...
      • 您希望您的布局看起来如何。您可以张贴图片吗?
      【解决方案7】:

      您所做的是正确的,但要查看更改,您只需重新启动 IDE。我找到了答案here

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2014-02-14
        • 1970-01-01
        • 2015-03-23
        • 1970-01-01
        • 2012-04-18
        • 2012-06-30
        相关资源
        最近更新 更多