TextView文本居中对齐

时间:2019-05-17 07:55:22

标签: android android-layout textview

我想将TextView中的文本对齐到中心。我使用了android:gravity="center",但单词之间留有额外的间距。

<LinearLayout
        android:id="@+id/textContainer"
        android:orientation="vertical"
        android:layout_weight="4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:textSize="24sp"
            android:textStyle="bold"
            android:textColor="@color/colorTextDarkGrey"
            android:gravity="center"
            android:text="INTRO TITLE"/>

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textStyle="normal"
            android:textColor="@color/colorTextLightGrey"
            android:gravity="center"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
            labore et dolore magna aliqua."/>

    </LinearLayout>

预期结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

看到单词间距的原因是您在文本中使用了多余的行,从而在incididunt utlabore et之间引入了空格。

只需替换一下(两行):

android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
            labore et dolore magna aliqua."

(一行):

android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

希望对您有帮助!

答案 1 :(得分:0)

如果必须固定TextView的文本,则只需消除任何新的换行符,否则,如果您要以编程方式执行此操作,则可以确保不传递带有换行符的字符串:

    @login_required
    def index(request):

        form = InfosForm(request.POST or None)

        if form.is_valid():

            #if request.user.is_authenticated:
            #    gituser = request.user.social_auth.filter(provider ='github-oauth2')
            #    extra_data = str(gituser.extra_data['access_token'])

            g = Github(gitkey)

            try:

                orga_name = form.cleaned_data['orga_name']
                org = g.get_organization(orga_name)
                repos = org.get_repos()
                reposList = []
                totalCommits = []
                #Transformation de la PaginatedList en liste classique pour manipuler   facilement les repos
                for repo in repos:
                    commitsList = repo.get_stats_participation().all
                    languages = repo.get_languages()
                    if not languages:
                        top_language = ''
                    else:
                        top_language = max(languages.keys(), key=(lambda key: languages[key]))
                    repository = Repository(titre=repo.name, starsgazers=repo.stargazers_count, top_language=top_language)
                    if len(commitsList) >= 2:
                        repository.commits_this_week = commits_this_week=commitsList[-1]
                        repository.commits_last_week = commits_last_week=commitsList[len(commitsList)-2]
                    reposList.append(repository)
                reposList.sort(key=lambda x: x.starsgazers, reverse=True)
                reposList = reposList[:10]
                #django jchart
                envoi = True

            except UnknownObjectException :
                erreur = True

        return render(request,'gitInsights/index.html', locals())

    def get_total_commits_last_week(reposList):

        totalCommits = []
        for repo in reposList:
            totalCommitsAllWeeks = repo.get_stats_participation().all
            totalCommits.append(totalCommitsAllWeeks[-1])

        return totalCommits

    @login_required
    def settings(request):
        user = request.user

        try:
            github_login = user.social_auth.get(provider='github')
        except UserSocialAuth.DoesNotExist:
            github_login = None

        can_disconnect = (user.social_auth.count() > 1 or user.has_usable_password())

        return render(request, 'settings.html', {
            'github_login': github_login,
            'can_disconnect': can_disconnect
        })

    @login_required
    def password(request):
        if request.user.has_usable_password():
            PasswordForm = PasswordChangeForm
        else:
            PasswordForm = AdminPasswordChangeForm

        if request.method == 'POST':
            form = PasswordForm(request.user, request.POST)
            if form.is_valid():
                form.save()
                update_session_auth_hash(request, form.user)
                messages.success(request, 'Your password was successfully updated!')
                return redirect('password')
            else:
                messages.error(request, 'Please correct the error below.')
        else:
            form = PasswordForm(request.user)
        return render(request, 'password.html', {'form': form})


    def informations_legales(request):
        return render(request, 'gitInsights/informations_legales.html')