简体   繁体   English

如何删除已经放大到屏幕的布局

[英]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.

相关问题 如何关闭充气布局? - How to close inflated layout? 我将视图膨胀了 5 次,膨胀的视图有一个 EditText,现在我在屏幕上有 5 个 EditText,如何从这些 EditTexts 中获取文本 - I inflated a view 5 times, the view which was inflated had a EditText, now i have 5 EditText on the screen, how to get the text from those EditTexts 放大后如何显示PopUpWindow - How to show PopUpWindow after it has been inflated 如何访问膨胀布局内的TextView的值 - How to access the value of a TextView that is inside an inflated layout 如何从布局中获取膨胀的片段引用? - How to get the inflated Fragment Reference from Layout? 如何以线性布局访问膨胀视图 - How to access inflated views in a linear layout 如何从另一个布局中的 Fragment 中找到 ViewById,而不是作为根布局膨胀的布局? - How to findViewById in a Fragment from another layout, not the one inflated as the root layout? setOnClickListener 不适用于膨胀布局 - setOnClickListener not working for inflated layout 片段布局未膨胀 - Fragment layout not being inflated android-如何在已经膨胀的布局中膨胀地图片段? - android - how to inflate a map fragment in an already inflated layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM