JSON文本在textview中不会自动调整大小(水平和垂直方向均一)

时间:2018-10-15 02:11:36

标签: java android

enter image description here

enter image description here

enter image description here

我正在尝试从Firebase数据库中上传的JSON文件中获取文本,但是当获取文本行时,它们不会自动调整大小(x和y轴为wrt),尽管我使用的是autoSizeTextType =以及添加的参数。右侧仍然存在空白(从快照中可以看到)。我已经使用了第三方Gradle依赖项,但是,它们也表现不佳。因此,请帮助我,并根据我的代码告诉我解决方案。

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CourseDetail">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:fitsSystemWindows="true"
            app:contentScrim="#262628"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/img_course"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@null"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:layout_collapseMode="parallax"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="Course Name"
                app:titleTextColor="#ffffff" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="1dp"
                app:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:padding="12dp"
                        android:text="DESCRIPTION"
                        android:textColor="#262628"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="1dp"
                app:cardUseCompatPadding="true">

                <TextView
                    android:id="@+id/course_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:autoSizeTextType="uniform"
                    android:fitsSystemWindows="true"
                    android:lineSpacingMultiplier="1.5"
                    android:text="Description "
                    android:textColor="@android:color/black"
                    android:autoSizeMinTextSize="15sp"
                    android:autoSizeMaxTextSize="20sp"
                    android:autoSizeStepGranularity="1sp"/>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="1dp"
                app:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:padding="12dp"
                        android:text="DEGREE INFROMATION"
                        android:textColor="#262628"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="1dp"
                app:cardUseCompatPadding="true">

                <TextView
                    android:id="@+id/degree_information"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:lineSpacingMultiplier="1.5"
                    android:padding="12dp"
                    android:text="Description"
                    android:textColor="@android:color/black"
                    android:textSize="14sp" />
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="1dp"
                app:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:padding="12dp"
                        android:text="JOB SCOPE"
                        android:textColor="#262628"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="1dp"
                app:cardUseCompatPadding="true">

                <TextView
                    android:id="@+id/job_scope"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:lineSpacingMultiplier="1.5"
                    android:padding="12dp"
                    android:text="Description"
                    android:textColor="@android:color/black"
                    android:textSize="14sp" />
            </android.support.v7.widget.CardView>
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

Activity.java

package com.example.shubhojit.careersafter10th;

import android.support.annotation.NonNull;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shubhojit.careersafter10th.Model.Courses_After10th;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;


public class CourseDetail extends AppCompatActivity {

    TextView course_description,deg_info,job_scope;
    ImageView courseImage;
    CollapsingToolbarLayout collapsingToolbarLayout;

    String courseId="";

    FirebaseDatabase database;
    DatabaseReference coursedetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);

        database = FirebaseDatabase.getInstance();
        coursedetail = database.getReference("Courses_After10th");

        course_description = (TextView)findViewById(R.id.course_description);
        deg_info = (TextView)findViewById(R.id.degree_information);
        job_scope = (TextView)findViewById(R.id.job_scope);
        courseImage = (ImageView)findViewById(R.id.img_course);

        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing);
        collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
        collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);

        if(getIntent() != null)
            courseId = getIntent().getStringExtra("CourseId");
        if(!courseId.isEmpty())
        {
            getDetailCourse(courseId);
        }
    }

    private void getDetailCourse(String courseId) {
        coursedetail.child(courseId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Courses_After10th course = dataSnapshot.getValue(Courses_After10th.class);

                Picasso.with(getBaseContext()).load(course.getImage()).into(courseImage);
                collapsingToolbarLayout.setTitle(course.getName());
                deg_info.setText(course.getDegree_Information());
                job_scope.setText(course.getJob_Scope());

                course_description.setSingleLine(false);

                course_description.setText(course.getDescription());
                course_description.setEllipsize(TextUtils.TruncateAt.END);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

TextView没有提供Word kinda工具中可用的Justify种对齐方式。

要使文本对齐,请对HTML内容使用WebView。使用正确的对齐方式和样式将文本转换为HTML标签。然后将内容作为HTML加载到WebView中。