首页 > 解决方案 > 片段内的Textview显示不相关的文本

问题描述

我是 android 应用程序开发的新手。我的问题是我试图在位于片段内的文本视图中显示阿拉伯文本。

细节:

就我而言;textview 放置了一些不相关的单词 - 这些单词不包含在传递的字符串中(必须显示的阿拉伯语文本)。我发现它与字体有关。因为不相关的文本会根据我使用的字体而变化。下面是我的应用程序的两个屏幕截图的链接。在其中一个你可以看到“null”字写在第一行的最左边。但是,如果我将字体更改为阿拉伯字体,则“空”字会将其位置留给阿拉伯字符串。

截图 1 截图2

我想摆脱这个“空”字,但我找不到怎么做。如果有人展示,我会很高兴。

阿拉伯语文本是根据对象“comingVird”的字符串变量指定的,该对象是我构建的“Vird.class”的实例。

这是我的片段类:

package com.codeforlite.virdlerim.Fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.codeforlite.virdlerim.R;

import java.util.Objects;

public class Fragment_Arabic_Image extends Fragment{

    private String imageName;
    private  View view;
    private TextView txt_oku_arabic_Text;
    private String arabicText;
    private Image image;

    public Fragment_Arabic_Image(String imageName,String arabicText) {
        this.imageName = imageName;
        this.arabicText=arabicText;
    }

    @SuppressLint("ResourceType")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        
        view=inflater.inflate(R.layout.fragment_arabic_image,container,false);

        //define textview 
      
        txt_oku_arabic_Text=view.findViewById(R.id.txt_oku_arabic_Text);
        txt_oku_arabic_Text.setText(arabicText);
 
        

        return view;
    }
    
}

这是我的活动类:(在活动内部;有一个带有三个片段的底部导航视图。其中一个片段“Fragment_Arabic_Image”有问题。)

package com.codeforlite.virdlerim;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.codeforlite.virdlerim.Fragments.Fragment_Anlam;
import com.codeforlite.virdlerim.Fragments.Fragment_Arabic_Image;
import com.codeforlite.virdlerim.Fragments.Fragment_Turkce_Okunus;
import com.codeforlite.virdlerim.Vird_Classes.AyetGrubu;
import com.codeforlite.virdlerim.Vird_Classes.Vird;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.Serializable;


/*this activity is to read a text with a counter- it shows arabic and turkish spelling and also meaning
in different fragments*/
public class Oku extends AppCompatActivity implements Serializable {

    private int actualNumber;
    private FragmentManager fragmentManager;
    private Fragment tempFragment;
    private BottomNavigationView bottomNavigationView;
    private FrameLayout fragmentContainer;
    private ProgressBar progressBar;
    private Button btn_tıkla;
    private Vird comingVird;
    private Fragment arabicImageFragment;
    private Fragment turkishTextFragment;
    private Fragment anlamFragment;
    private MediaPlayer clickSoundPlayer;
    private MediaPlayer completeSoundPlayer;
    private Vibrator vb;
    private SharedPreferences kalanSayiKayit;
    private SharedPreferences.Editor kalanSayiKayitEditor;

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

        //to save the number at the counter
        kalanSayiKayit=getSharedPreferences("kalansayilar",MODE_PRIVATE);
        kalanSayiKayitEditor=kalanSayiKayit.edit();

        //initialize views
        fragmentContainer=findViewById(R.id.fragment_container);
        bottomNavigationView=findViewById(R.id.bottom_navi);
        progressBar=findViewById(R.id.progressBar);
        btn_tıkla=findViewById(R.id.button_tıkla);

        //sound at every button click
        clickSoundPlayer=MediaPlayer.create(this,R.raw.click);

        //sound will be played when target reached
        completeSoundPlayer=MediaPlayer.create(this,R.raw.complete);

        //vibrator for button click
        vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        //coming vird with the intent
        comingVird= (Vird) getIntent().getSerializableExtra("Vird.class");

        actualNumber=comingVird.getTargetNumber();

        //put the arabic image fragment on the container first
        arabicImageFragment=new Fragment_Arabic_Image(comingVird.getImageName(),comingVird.getArabicText());
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,arabicImageFragment).commit();

        //eğer gelen vird ayet ise Anlam Sekmesini Meal Sekmesi yap
        if(comingVird.getClass().getSimpleName().equals("AyetGrubu")){bottomNavigationView.getMenu().getItem(2).setTitle("Meal");}


        //on navigation item selected:
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                //get the selected items id number
                int itemID=item.getItemId();

                //if id number is related to the arabic image fragment, set the temp fragment as arabic image fragment
                if(itemID==R.id.action_oku_arapca){
                    tempFragment=new Fragment_Arabic_Image(comingVird.getImageName(),comingVird.getArabicText());
                }

                //if id number is related to turkish text fragment, set the related fragment
                else if(itemID==R.id.action_oku_turkce) { tempFragment=new Fragment_Turkce_Okunus(comingVird.getTurkishText());}

                //if id number is related to the meaning set meaning fragment..
                else if(itemID==R.id.action_oku_anlamı){

                    //to change the text on the bottom navigation bar item
                    if(comingVird.getClass().getSimpleName().equals("AyetGrubu")){
                        AyetGrubu ayetGrubu=(AyetGrubu)comingVird;
                        tempFragment=new Fragment_Anlam(ayetGrubu.getMeal());}
                    }

                    else{
                         tempFragment=new Fragment_Anlam(comingVird.getMeaning());
                    }


                //replace the fragment on the container with the temp fragment
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,tempFragment).commit();

                return true;

                }
            });

        //initiate progress bar with full progress
        progressBar.setProgress(100);

        //show the number of the counter on the button
        btn_tıkla.setText(""+actualNumber);

        //on button click
        btn_tıkla.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(actualNumber<=1) {
                    progressBar.setProgress(0);
                    btn_tıkla.setText("Tebrikler\nhedefinize ulaştınız!");
                    btn_tıkla.setTextSize(15);
                    vb.vibrate(500);
                    completeSoundPlayer.start();

                }

                else{
                    actualNumber--;
                    kalanSayiKayitEditor.putInt(comingVird.getId(),actualNumber);
                    kalanSayiKayitEditor.commit();

                    progressBar.setProgress(100*actualNumber/comingVird.getTargetNumber());
                    //decrease the number and set on the text of the button

                    btn_tıkla.setText("" + actualNumber);

                    clickSoundPlayer.start();

                    //her button tıklamada kısa titreşim ver
                    vb.vibrate(100);
                }
            }
        });

    }

}

这是片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/fragment_arabic_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:fillViewport="true"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:orientation="vertical"

            tools:ignore="MissingConstraints">

            <TextView
                android:id="@+id/txt_oku_arabic_Text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/quran"
                android:gravity="center"
                android:inputType="textMultiLine"
                android:justificationMode="inter_word"
                android:padding="20dp"
                android:scrollbars="vertical"
                android:textColor="@color/primary_text"
                android:textSize="30sp"
               
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-fragmentstextview

解决方案


推荐阅读