[英]How to remove a layout that have been inflated to the screen
I've created an in-app keyboard with two possible languages. 我用两种可能的语言创建了一个应用内键盘。
For each language there's a different layout (the root element of the layouts is merge). 对于每种语言,都有一个不同的布局(布局的根元素是merge)。 I have a 'switch' Button in every layout for switching between the languages.
我在每种布局中都有一个“切换”按钮,用于在语言之间进行切换。
I've tried to set the layout that currently shown as 'GONE' and then add the second layout, but it doesn't work and instead of changing the layout, it just adding the second layout, and showing it with the second together. 我试图设置当前显示为'GONE'的布局,然后添加第二个布局,但是它不起作用,并且没有更改布局,它只是添加了第二个布局,并与第二个一起显示。
Here is the Class that handles my keyboard. 这是处理我的键盘的类。
The place that I handle the language switching is found at the Switch-Case inside the function onClick, case: 'R.id.button_switch': 我处理语言切换的地方是在onClick函数内的Switch-Case中找到的,案例:“ R.id.button_switch”:
public class MyKeyboard extends LinearLayout implements View.OnClickListener {
private SparseArray<String> keyValues = new SparseArray<>();
private InputConnection inputConnection;
private Boolean isEnglish;
private Context con;
private View keb;
public MyKeyboard(Context context) {
this(context, null, 0);
}
public MyKeyboard(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
con = context;
//english(context);
hebrew(context);
}
private void hebrew(Context context) {
isEnglish = false;
keb = LayoutInflater.from(context).inflate(R.layout.heb_keyboard, this, true);
findViewById(R.id.button_enter).setOnClickListener(this);
findViewById(R.id.button_delete).setOnClickListener(this);
findViewById(R.id.button_switch).setOnClickListener(this);
findViewById(R.id.button_alef).setOnClickListener(this);
findViewById(R.id.button_b).setOnClickListener(this);
findViewById(R.id.button_c).setOnClickListener(this);
keyValues.put(R.id.button_alef, "א");
keyValues.put(R.id.button_b, "b");
keyValues.put(R.id.button_c, "c");
}
private void english(Context context) {
isEnglish = true;
keb = LayoutInflater.from(context).inflate(R.layout.en_keyboard, this, true);
findViewById(R.id.button_enter).setOnClickListener(this);
findViewById(R.id.button_delete).setOnClickListener(this);
findViewById(R.id.button_switch).setOnClickListener(this);
findViewById(R.id.button_a).setOnClickListener(this);
findViewById(R.id.button_b).setOnClickListener(this);
findViewById(R.id.button_c).setOnClickListener(this);
keyValues.put(R.id.button_a, "a");
keyValues.put(R.id.button_b, "b");
keyValues.put(R.id.button_c, "c");
}
@Override
public void onClick(View view) {
if (inputConnection == null)
return;
switch (view.getId()) {
case R.id.button_delete:
if (TextUtils.isEmpty(inputConnection.getSelectedText(0))) {
inputConnection.deleteSurroundingText(1, 0);
} else {
inputConnection.commitText("", 1);
}
break;
case R.id.button_enter:
inputConnection.finishComposingText();
this.setVisibility(INVISIBLE);
break;
case R.id.button_switch:
if (isEnglish)
hebrew(con);
else
english(con);
break;
default:
inputConnection.commitText(keyValues.get(view.getId()), 1);
break;
}
}
public void setInputConnection(InputConnection ic) {
inputConnection = ic;
}
}
I need to find how to remove the previews layout that were inflated before, and then add the other layout. 我需要找到如何删除之前膨胀的预览布局,然后添加其他布局。
如果setVisibility(GONE)
未解决,则可以使用FrameLayout
并通过以下方式更改其片段:
getSupportFragmentManager().beginTransaction().replace(R.id.main_container, fragment, newTag).commit();
尝试这个
View view = inflate(mContext, R.layout.en_keyboard, this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.