我正在尝试构建一个首选项屏幕,其中包含一些带有一些TextView和EditText首选项的布局。
当我在Activity中使用textView.setText(inputString)时,它不会覆盖现有的TextView,而是使用inputString作为文本创建TextView的新副本,并且它与现有的TextView重叠。是否有任何方法可以使setText覆盖现有的TextView本身。
这是带有TextView的XML布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@color/blue"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
></ListView>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:textColor="@color/pseudoBlack"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:layout_marginTop="100dp"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:textColor="@color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:layout_marginStart="100dp" />
<TextView
android:text="TextView"
android:textColor="@color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="@+id/textView11"
android:layout_below="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:layout_marginStart="21dp" />
</RelativeLayout>
这是我正在使用的首选屏幕。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
>
<Preference android:layout="@layout/inner" android:key="innerkey"
/>
<Preference android:key="mskey"
android:title="show"
/>
<EditTextPreference android:key="editpref"
android:title="Add"
/>
</PreferenceScreen>
这是我尝试做setText()的Activity类。
package com.example.asus.layoutcheck;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.TextView;
public class SecondaryActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner);
TextView txt1 = (TextView)findViewById(R.id.textView2);
TextView txt2 = (TextView)findViewById(R.id.textView5);
TextView txt3 = (TextView)findViewById(R.id.textView11);
txt1.setText("First Text");
txt2.setText("Second Text");
txt3.setText("Third Text");
final String message = "This is the message" ;
addPreferencesFromResource(R.xml.pref);
final Preference msg = findPreference("mskey");
msg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
msg.setSummary(message);
return true;
}
});
Preference edit = findPreference("editpref");
}
}
我如何克服这个问题?提前致谢
答案 0 :(得分:0)
将setContentView(R.layout.inner);
替换为addPreferencesFromResource(//your preferenceScreen);
答案 1 :(得分:0)
我也遇到过类似的问题,它发生在其他视图以及FrameLayout内部的TextureView中。我将我的TextureView调整为较小的尺寸,但仍能在后面的闪烁中看到以前的TextureView。
使它工作的原因很有趣->我只是在布局(LinearLayout)中添加了黑色背景色(可能是任何颜色),现在它可以正常工作。
我添加了:android:background =“#000000”
<LinearLayout 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"
android:background="#000000"
android:orientation="vertical"
android:visibility="visible"
tools:context=".CameraActivity">
如果不知道用什么颜色清除背景,似乎背景没有清除。