Android Horizo​​ntalScrollView不显示所有子视图

时间:2013-01-15 11:44:08

标签: android user-interface layout view horizontalscrollview

我有一个包含水平列表视图的android活动。下面是它的布局:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id ="@+id/topMostLayout"
                android:layout_width="fill_parent"
    android:layout_height="fill_parent"
                android:background="@color/grey">
    <HorizontalScrollView
      android:id ="@+id/horScrollView"
       android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      >
      <LinearLayout
android:id ="@+id/dateRibbon"
 android:orientation="horizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
       android:layout_marginLeft="50dp"
       android:layout_marginRight="50dp"
                    android:gravity="center"
                    >
      </LinearLayout>
    </HorizontalScrollView>
  <TextView
    android:id="@+id/line"
    android:paddingTop="5dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="5dp" 
    android:background ="#37000000"
      android:layout_below="@id/horScrollView"
    />

  <LinearLayout
       android:id="@+id/bottom_control_bar"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true" >
  </LinearLayout>
  <ListView
      android:id="@android:id/list"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_above="@id/bottom_control_bar"
      android:layout_below="@id/line" >
  </ListView>
  <TextView
      android:id="@android:id/empty"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@id/bottom_control_bar"
      android:layout_below="@id/horScrollView"
      android:text="@string/tasks_empty" />

</RelativeLayout>

问题是当我向horizo​​ntalscrollview内的linearlayout添加视图时,它不会完全向右滚动,因此最右边的子视图对用户是不可见的。我将textviews添加到linearlayout。下面是填充线性布局的代码:

 LinearLayout dateRibbon = FindViewById<LinearLayout>(Resource.Id.dateRibbon);

            dateRibbon.RemoveAllViews();

            TextView tView;

            m_dateRibbonTextViews = new List<TextView>();
            m_dateRibbonDates = new List<DateTime>();

            int currentJobId = Telecetera.Connect.JobLibrary.JobData.Job.GetCurrentJobID();
            int defaultCurrentJobID = Telecetera.Connect.JobLibrary.JobData.Job.SYSDIR_CURRENTJOB_DEFAULT;


            Telecetera.Connect.JobLibrary.JobData.JobDetailsList jobDetailsList;
            int i = -1;
            foreach (DateTime date in jobsByDay.Keys)
            {
                ++i;

                jobDetailsList = jobsByDay[date];

                tView = new TextView(this);
                tView.Gravity = GravityFlags.CenterHorizontal;
                tView.Text = GetDateDisplayString(date);
                tView.SetTextSize(Android.Util.ComplexUnitType.Sp, 18);
                tView.SetPadding(10, 0, 10, 0);

                if (currentJobId != defaultCurrentJobID && jobDetailsList.GetByJobID(currentJobId) != null)
                {
                    m_indicesDaysWithCurrentJob.Add(i);
                    tView.SetBackgroundColor(Android.Graphics.Color.Cyan);
                }
                else
                {
                    tView.SetBackgroundColor(Android.Graphics.Color.White);
                }

                tView.Click += new EventHandler(tView_Click);
                dateRibbon.AddView(tView);
                m_dateRibbonTextViews.Add(tView);
                m_dateRibbonDates.Add(date);

                tView = new TextView(this);
                tView.SetPadding(2, 0, 2, 0);
                tView.SetBackgroundColor(Resources.GetColor(Resource.Color.grey));
                dateRibbon.AddView(tView);

            }

任何关于为什么它没有完全滚动到右边的提示将不胜感激! 感谢。

2 个答案:

答案 0 :(得分:1)

我能够通过删除

解决它
android:layout_marginLeft="50dp"
       android:layout_marginRight="50dp"

行。

答案 1 :(得分:0)

你可以将填充设置为线性布局这里是我的代码,它显示中心的文本视图,并在开始和视图结束时给出一些空间。

lLayTwo.setPadding((center - 2 - centerScreenChildLeft), 0, (center - 2 - centerScreenChildRight), 0);

private void addSubMenu(ArrayList<String> list,String mainMenu){
    this.mainMenu = mainMenu;
    lLayTwo.removeAllViews();

    mViewFlipper.setDisplayedChild(mViewFlipper.indexOfChild(mViewPlain));

    menu = list;
    Log.d(TAG, "meulist "+menu.size());
    range = list.size() * 2 + 1;
    Log.d(TAG, "range "+range);
    int menuItem = 0;

    for ( int i = 0; i < range; i++ ) {
        if (  i % 2 == 0 ){

            View  txt = new View(MCSDistrictMainActivity.this);
            txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            txt.setBackgroundResource(R.drawable.line);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 0, 0);
            lLayTwo.addView(txt,params);

        }else{

            TextView txt = new TextView(MCSDistrictMainActivity.this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(parentChildWidh, LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 8, 0, 5);
            params.gravity = Gravity.CENTER;
            txt.setText(menu.get(menuItem));
            txt.setTag(menu.get(menuItem));
            txt.setTextColor(getResources().getColor(R.color.violet));
            txt.setTextSize(15f);
            txt.setGravity(Gravity.CENTER_HORIZONTAL);
            menuItem += 1;
            lLayTwo.addView(txt,params);
        }

    }

    Log.d(TAG, "llayout Two "+lLayTwo.getChildCount()+" width "+parentChildWidh+" LEFT "+parentChildLeft);

    int left = parentChildLeft;
    int width = parentChildWidh;
    int centerTab = 1 + hsvLowerTab.getWidth() / 2;
    Log.d(TAG, "Measures left "+left+" width "+width+" center "+centerTab+" all "+( left + width -centerTab ));
    //(left + width/2)-centerTab
    hsvLowerTab.scrollTo((left + width/2)-centerTab, 0);
    hsvLowerTab.dispatchTouchEvent (MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), 
                                    MotionEvent.ACTION_UP,(left + width/2)-centerTab, 0, 0));

}