简体   繁体   English

谁能告诉我为什么此文本不在Android Studio中居中?

[英]Can Anyone Tell Me Why This Text Is Not Centering In Android Studio?

I'm attempting to align the text as you would expect to see if you were in a word pad and set your alignment to center, the middle of each line would be in the center of the screen and each margin to the left and right would be equidistant. 我正在尝试对齐文本,就像您希望看到的那样,如果您在写字板中,并将对齐方式设置为居中,则每行的中间将位于屏幕的中心,而左右各边距将等距。 However, using gravity does not appear to be working as the text is still starting from the left. 但是,使用重力似乎无效,因为文本仍从左侧开始。 Could someone shed light as to why? 有人能解释为什么吗? Here's the xml. 这是xml。

<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:paddingLeft="3dp"
    android:paddingRight="3dp"
    android:paddingTop="3dp"
    android:paddingBottom="3dp"
    android:orientation="vertical"
    android:background="#111111"
    tools:context="eqlogic.annswingsandthings.AboutUs">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/chalkBoard"
        android:src="@drawable/ann_chalkboard_img"
        android:layout_weight="0.5"/>
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:id="@+id/scrollView"
        android:layout_gravity="center_horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:textColor="#999999"
            android:text="@string/AboutUsText"
            android:textSize="24sp"
            android:id="@+id/textView"
            android:layout_gravity="center_horizontal"
            android:singleLine="false" />
    </ScrollView>
</LinearLayout>

You are not setting the gravity, use: 您没有设置重力,请使用:

android:gravity="center"

Full tag: 完整标签:

<TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:textColor="#999999"
        android:text="@string/AboutUsText"
        android:textSize="24sp"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:singleLine="false" />

Set also android:gravity parameter in ScrollView to center . 还将ScrollView android:gravity参数设置为center

For testing the effects of different layout parameters I recommend to use different background color for every element, so you can see how your layout changes with parameters like gravity, layout_gravity or others. 为了测试不同布局参数的效果,我建议为每个元素使用不同的背景色,这样您就可以看到布局如何随着重力,layout_gravity或其他参数而变化。

Make your layout_width be fill_parent and use gravity instead of layout_gravity : 使您的layout_widthfill_parent并使用重力代替layout_gravity

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:textColor="#999999"
        android:text="@string/AboutUsText"
        android:textSize="24sp"
        android:id="@+id/textView"
        android:gravity="center"
        android:singleLine="false" />

设置滚动视图参数Android:layout_width =“ match_parent”和Android:gravity =“ center”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能告诉我如何使用android studio将地址解析器地址存储在android数据库中? - can anyone tell me how to store geocoder address in android database using android studio? 谁能告诉我为什么这个where子句没有给我结果? - Can anyone tell me why this where clause is not giving me the result? 谁能告诉我为什么我在logcat中收到此错误? - Can anyone tell me why do I get this error in logcat? 任何人都可以告诉我为什么发生以下错误-方向错误 - Can Anyone tell me why the following error occurs - wrong orientation 谁能告诉我为什么单击按钮后什么都没有发生? - Can anyone tell me why nothing is happen on the click of button? 谁能告诉我为什么所有Rect都会触发相同的活动 - Can Anyone Tell me Why All Rects fires to same Activity 谁能告诉我有关Android的InternalStorage的信息? 我很困惑 - Can anyone tell me about InternalStorage in Android ? I am confused 任何人都可以告诉我我的 Android Studio Java 代码有什么问题...正在尝试实现底部导航栏 - Can Anyone Tell me whats the problem in my Android Studio Java Code... Trying to implement bottom Navigation Bar 谁能告诉我playReady的哑剧类型? - can anyone tell me the mime type of the playReady? 谁能告诉我这个小部件的名称是什么? - Can anyone tell me what is the name of this widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM