簡體   English   中英

如何刷新以編程方式創建的布局?

[英]How to refresh layout that is created programmatically?

我有一種情況,就像我在onCreateView()編程方式創建自定義布局。 例如 :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return createLayout(cView);
    }

    View createLayout(View view) {
    LinearLayout linLayout = new LinearLayout(activity);
    linLayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    linLayout.setLayoutParams(linLayoutParam);

    for (int i = 0; i <= 3; i++) {
         TextView tv = new TextView(activity);
         tv.setId(i);
         tv.setText("TextView");
         tv.setLayoutParams(lpView);
         linLayout.addView(tv);
    }

    view = linLayout;

    return view;
    }

    @Override
    public void onResume() {
    super.onResume();

    }

不,我要問的是如何從onResume()刷新具有ID的textview 謝謝。

您可以使用textview的ID鍵將您的textview保持在哈希圖中。 然后在您的簡歷上。 您可以通過id獲取該textview並刷新它。

private HashMap<Integer,TextView> textViews = new HashMap<>();

在創建此textview的on create方法中,需要將textview放入hashmap中。

for (int i = 0; i <= 3; i++) {
    TextView tv = new TextView(activity);
    tv.setId(i);
    tv.setText("TextView");
    tv.setLayoutParams(lpView);
    linLayout.addView(tv);
    textViews.put(i,tv);
}

您的onResume方法應如下所示

@Override
protected void onResume() {
    super.onResume();
    TextView tv = textViews.get(yourId);
    tv.setText("Your Text");
}

TextView電視設置為全局電視,然后照常更新該電視。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM