0

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

            }

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

4

2 回答 2

1

我能够通过删除

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

线。

于 2013-01-15T13:43:38.280 回答
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));

}
于 2013-01-15T12:17:13.060 回答