方向更改时TextView文本重叠

时间:2017-10-05 20:02:10

标签: android textview android-orientation overlapping

我有一个TextView用于描述,另一个用于当前步骤“app for 食谱“

当我将设备的方向更改为横向并再次返回纵向模式时,会发生此问题(TextView中的文本与另一个TextView不同,尽管有单个TextView且发生此重叠)请参阅附加图像。

Normal view before changing orientation

This when i go landscape and returned to portrait again

Xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

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

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
   />

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_gravity="left"
        android:paddingRight="15dp"
        android:elevation="1dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="16dp"
        android:textSize="17sp"
        android:text="intro"
        android:freezesText="true"
        android:gravity="center_horizontal"

        />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_next"
            android:layout_alignParentRight="true"
            app:fabSize="auto" />


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_back"
            app:fabSize="auto" />

        <TextView
            android:id="@+id/currentStep"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="1/10"
            android:textSize="20sp"/>


    </RelativeLayout>

</LinearLayout>

Java代码:

public class StepDetailsFragment extends android.app.Fragment implements View.OnClickListener {

ArrayList<Step> steps;

StepsAdapter adapter;

@BindView(R.id.description)
TextView description;
@BindView(R.id.currentStep)
TextView current;
@BindView(R.id.next_button)
FloatingActionButton next;
@BindView(R.id.back_button)
FloatingActionButton back;

FragmentOneListener listener;

public int currentIndex;
@BindView(R.id.playerView)
 SimpleExoPlayerView playerView;
SimpleExoPlayer player;

private boolean playWhenReady=true;
private long playbackPosition;
private int currentWindow;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.details_fragment, container, false);

    ButterKnife.bind(this, root);

    Bundle bundle = getArguments();
    if (!bundle.containsKey("steps")) {
        return root;
    }
    steps = bundle.getParcelableArrayList("steps");
    currentIndex = bundle.getInt("current");

    show();

    back.setOnClickListener(this);
    next.setOnClickListener(this);

    return root;
}

@Override
public void onClick(View v) {
    int id = v.getId();

    if (id == R.id.back_button) {
        currentIndex--;
        show();
    } else if (id == R.id.next_button) {
        currentIndex++;
        show();
    }

}


public void show() {
    if (currentIndex <= 0) {
        back.setVisibility(GONE);
    } else {
        back.setVisibility(View.VISIBLE);
    }
    if (listener != null) {
        listener.setCurrent(currentIndex);
    }
    if (currentIndex >= steps.size() - 1) {
        next.setVisibility(GONE);
    } else {
        next.setVisibility(View.VISIBLE);
    }

    description.setText(steps.get(currentIndex).getDescription());
    current.setText((currentIndex + 1) + "/" + steps.size());

}

1 个答案:

答案 0 :(得分:0)

您的活动会在轮播时重新创建。 片段管理器将原始片段存储在实例状态中,但我假设您在活动的onCreate()中添加了一个新片段。 因此,您将已恢复的片段和新片段放在一起。

如果savedInstanceStatenull,则可能只会添加新片段。