Android - 从线性布局中删除膨胀的布局xml

时间:2016-05-05 15:15:10

标签: android xml android-layout android-studio

我将internetnotconnected.xml虚拟化为线性布局。我想在用户点击按钮时删除View夸大的布局。但我的方式没有用。

if (!EZInternetConnection.isNetworkConnected(context)) {
            LinearLayout LLPureCard = (LinearLayout) ((Activity) context).findViewById(R.id.ll_main_activity_pure_card);
            LinearLayout LLPureCardContent = (LinearLayout) ((Activity) context).findViewById(R.id.ll_main_activity_pure_card_content);
            LLPureCardContent.setVisibility(View.GONE);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.internetnotconnected, LLPureCard);
            Button button = (Button) ((Activity) context).findViewById(R.id.b_internet_not_connected_try_connection);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (EZInternetConnection.isNetworkConnected(context)) {

                        LinearLayout LLPureCard = (LinearLayout) ((Activity) context).findViewById(R.id.ll_main_activity_pure_card);
                        LinearLayout LLPureCardContent = (LinearLayout) ((Activity) context).findViewById(R.id.ll_main_activity_pure_card_content);
                        int id = context.getResources().getIdentifier("internetnotconnected", "layout", context.getPackageName());
                        LLPureCard.removeView(((Activity) context).findViewById(id));
                        LLPureCardContent.setVisibility(View.VISIBLE);
                        Get20Words();
                    }
                }
            });
            this.onCancelled();
        }

2 个答案:

答案 0 :(得分:5)

我会尝试这样做,替换

inflater.inflate(R.layout.internetnotconnected, LLPureCard);

final View addedView = inflater.inflate(R.layout.internetnotconnected, null);
LLPureCard.addView(addedView);

然后在onClick方法中替换

LLPureCard.removeView(((Activity) context).findViewById(id));

使用

LLPureCard.removeView(addedView);

答案 1 :(得分:0)

尝试替换

int id = context.getResources().getIdentifier("internetnotconnected", "layout", context.getPackageName());

通过

int id = context.getResources().getIdentifier("internetnotconnected", "id", context.getPackageName());