【问题标题】:Linear layout children not scrolling线性布局子级不滚动
【发布时间】:2015-06-29 23:20:57
【问题描述】:

我正在尝试在运行时将图像视图添加到线性布局。

<LinearLayout
    android:id="@+id/llFolderLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".65"
    android:orientation="horizontal"
    android:scrollbars="horizontal" >
</LinearLayout>
LinearLayout llFolderTabs = (LinearLayout) myfolder.findViewById(R.id.llFolderLayout);
ImageView imgTab = new ImageView(activity);
imgTab.setId(i);
imgTab.setTag(i);
imgTab.setImageDrawable(
    mContext.getResources().getDrawable(R.drawable.icon_folder_inactive));
if (i == 0) {
    imgTab.setImageDrawable(
        mContext.getResources().getDrawable(R.drawable.icon_folder_active));
    // imgTab.setPadding(10, 0, 4, 0);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        74, LayoutParams.MATCH_PARENT);
    lp.setMargins(15, 0, 0, 0);
    imgTab.setLayoutParams(lp);
} else {
    // imgTab.setPadding(4, 0, 4, 0);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        74, LayoutParams.MATCH_PARENT);
    lp.setMargins(30, 0, 0, 0);
    imgTab.setLayoutParams(lp);
}
llFolderTabs.addView(imgTab);

但是,当孩子的数量超过屏幕大小时,它不会水平滚动 - 我怎样才能达到这种效果?

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    ScrollView 是一种特殊的布局,旨在容纳比实际尺寸更大的视图。当 Views 大小超过 ScrollView 大小时,会自动添加滚动条,可以垂直滚动。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp"
        android:fillViewport="false">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
      </LinearLayout>
    </ScrollView>
    

    http://javatechig.com/android/android-scrollview-example

    http://www.androidhub4you.com/2012/09/horizontal-scroll-view-in-android.html

    【讨论】:

      【解决方案2】:

      您可以使用ScrollView 进行垂直滚动或使用HorizontalScrollView 进行水平滚动。 ScrollView 只能有一个根视图,因此请将您的 LinearLayout 放在 scrollView 中,然后像您所做的那样将图像添加到 linearlayout 中。

      【讨论】:

        【解决方案3】:

        您应该将LinearLayout 包装在ScrollView 中。将ScrollView 设为您的xml 中的根元素。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多