【问题标题】:Android HorizontalScrollView does not display all the child viewsAndroid Horizo​​ntalScrollView 不显示所有子视图
【发布时间】:2012-12-29 12:10:16
【问题描述】:

我有一个包含水平列表视图的 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>

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

 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);

            }

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

【问题讨论】:

    标签: android user-interface layout view horizontalscrollview


    【解决方案1】:

    我可以通过删除来解决它

    android:layout_marginLeft="50dp"
           android:layout_marginRight="50dp"
    

    行。

    【讨论】:

    • 我以同样的方式解决了它,行的根视图中没有边距。可悲的是,我后来才读到这篇文章:-)
    【解决方案2】:

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

    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));
    
    }
    

    【讨论】:

    • 感谢您的帮助纳赛尔。我能够通过删除 android:layout_marginLeft="50dp" android:layout_marginRight="50dp" 来解决它
    猜你喜欢
    • 2018-06-20
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多