簡體   English   中英

在Android中以編程方式添加視圖

[英]Adding views programmatically in Android

是否可以將視圖添加到與OnCreate()中的setContentView()中調用的布局不同的布局?

我試圖使用Zylincs ViewPager(在http://www.zylinc.com/blog-reader/items/viewpager-page-indicator.html上找到),我有那個部分設置並且工作得很好。

這給我留下了4個布局。 1是main.xml,其他三個是main_pg0.xml,main_pg1.xml和main_pg3.xml

這三個頁面是每個“頁面”的內容所在的位置,其中main.xml包含頁面查看器。

在onCreate中使用setContentView(main.xml)。

我現在嘗試做的是能夠以編程方式將textViews添加到某些頁面。

我現在擁有的是:

LayoutInflater factory = getLayoutInflater();

View layout = factory.inflate(R.layout.main_pg0, null);
View linearLayout = layout.findViewById(R.id.linearLayout);

TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

((LinearLayout) linearLayout).addView(valueTV);

這是我能夠得到的,它根本不添加textview。

- - 編輯 - -

希望更多地澄清我試圖做什么繼承我的代碼

Main.java

public class Main extends FragmentActivity implements PageInfoProvider {
  public GlobalVars globalVars;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    globalVars = (GlobalVars) getApplicationContext();

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    ViewPagerIndicator indicator = (ViewPagerIndicator) findViewById(R.id.indicator);
    myPager.setOnPageChangeListener(indicator);

    indicator.init(0, adapter.getCount(), this);

    Resources res = getResources();
    Drawable prev = res.getDrawable(R.drawable.indicator_prev_arrow);
    Drawable next = res.getDrawable(R.drawable.indicator_next_arrow);

    indicator.setArrows(prev, next);
  }

  private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
      return 3;
    }

    public Object instantiateItem(View collection, int position) {

      LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      int resId = 0;
      switch (position) {
      case 0:
        resId = R.layout.main_pg0;
        LoadVehicles();
        break;
      case 1:
        resId = R.layout.main_pg1;
        break;
      case 2:
        resId = R.layout.main_pg2;
        break;
      }

      View view = inflater.inflate(resId, null);
      ((ViewPager) collection).addView(view, 0);

      return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
      ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public void finishUpdate(View arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == ((View) arg1);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
      // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public void startUpdate(View arg0) {
      // TODO Auto-generated method stub

    }

  }

  public String getTitle(int position) {
    String title = "--Untitled--";
    switch (position) {
    case 0:
      title = "Page 0";
      break;
    case 1:
      title = "Page 1";
      break;
    case 2:
      title = "Page 2";
      break;
    }
    return title;
  }
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <com.zylinc.view.ViewPagerIndicator
    android:id="@+id/indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#EAEAEA"
    android:paddingBottom="8dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="8dp" />
  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffeaeaea" >
    <View
      android:layout_width="fill_parent"
      android:layout_height="1dp"
      android:layout_alignBottom="@+id/current"
      android:background="#C5C5C5" />
    <ImageView
      android:id="@+id/current"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerInParent="true"
      android:src="@drawable/indicator_current" />
  </RelativeLayout>
  <android.support.v4.view.ViewPager
    android:id="@+android:id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

main_pg0.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FFFFFF"
  android:orientation="vertical"
  android:id="@+id/linearLayout" >
  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5dp"
      android:text="main_pg0.xml"
      android:textColor="#0C0C0C"
      android:textSize="18sp" />

</LinearLayout>

我想要做的是以編程方式將文本視圖添加到main_pg0.xml,同時僅保留其他2個頁面

你必須在主視圖中添加視圖,這是一個膨脹的布局。所以只需添加一行。

LayoutInflater factory = getLayoutInflater();

View layout = factory.inflate(R.layout.main_pg0, null);
View linearLayout = layout.findViewById(R.id.linearLayout);

TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

((LinearLayout) linearLayout).addView(valueTV);
layout.addView(linearLayout);

您還沒有明確提到是否要將FragmentViewPager ,但是由於您正在使用Zylinc實現,我將假設您是。 事實上,我只是使用提供的示例代碼向您展示這個想法。

了解如何實現您所追求的目標的關鍵是使ViewPager中的頁面成為Fragment形式的自包含單元。 它們是可重用的,有自己的生命周期,可以處理自己的布局。 因此,如果要對頁面布局進行運行時修改,則需要在構成這些頁面的Fragment內執行此操作。

可以在示例代碼的ItemFragment類中找到以下方法。 我只是將您自己的代碼片段中的TextView添加到它。 出於演示目的,我確實隱藏了用於填充date_fragment.xml布局文件中所有空間的ListView

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.date_fragment, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView)tv).setText(readableDateFormat.format(date));

    // changes below
    TextView valueTV = new TextView(getActivity());
    valueTV.setText("hallo hallo");
    valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    ((ViewGroup)v).addView(valueTV);

    return v;
}

那將直接在頁面中的日期下面給你“hallo hallo”文本(你是Dutchie?;)):

代碼段的結果

順便說一下,您可能需要考慮切換到Jake的ViewPagerIndicator 雖然我沒有仔細看過你正在使用的那個,但是Jake對我來說似乎更靈活,更清潔。 當然是你自己。


編輯:好的,所以你實際上並沒有在ViewPager使用片段,而是在為它提供布局。 這很好,雖然當頁面變得更復雜時,管理可能會變得更難。 無論如何,在運行時更改頁面布局的關鍵仍然是在構建/充氣時進行。 使用片段時,這將是重寫方法,如上所示。 但是,在您的情況下,您需要直接在instantiateItem(...)

我實際上並沒有在IDE中輸出這個,所以請注意和輕微的語法錯誤。

public Object instantiateItem(View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = null;
    switch (position) {
    case 0:
        view = inflater.inflate(R.layout.main_pg0, null);
        TextView valueTV = new TextView(getActivity());
        valueTV.setText("hallo hallo");
        valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        ((ViewGroup)view).addView(valueTV);
        break;
    case 1:
        view = inflater.inflate(R.layout.main_pg1, null);
        // or perhaps better for readability: build the layout in a different method, passing in the root
        buildSecondPageLayout(view);
        break;
    case 2:
        view = inflater.inflate(R.layout.main_pg2, null);
        buildThirdPageLayout(view);
        break;
    }

    return view;
}

暫無
暫無

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

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