【问题标题】:Android Why the Horizontal scroll does not fit the whole widthAndroid 为什么水平滚动不适合整个宽度
【发布时间】:2013-02-05 16:59:48
【问题描述】:

这是我的问题的 ListView 屏幕截图:

这是布局 XML:

<LinearLayout 
    android:id="@+id/viewer_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/viewer_filter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@android:drawable/ic_menu_search"
        android:hint="@string/hint_filter"
        android:background="@android:color/white"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="3dp"
        android:inputType="text"
        android:paddingLeft="4dp"
        android:selectAllOnFocus="true" >
    </EditText>

    <EditText
        android:id="@+id/viewer_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@android:drawable/ic_menu_search"
        android:hint="@string/hint_search"
        android:background="@android:color/white"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="5dp"
        android:inputType="text"
        android:paddingLeft="4dp"
        android:selectAllOnFocus="true" >
    </EditText>
</LinearLayout>

<HorizontalScrollView
    android:id="@+id/viewer_hscroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/viewer_top" >
    <ListView
        android:id="@+id/viewer_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>
</HorizontalScrollView>

这个场景有3个问题:

  1. 水平滚动视图没有覆盖整个屏幕宽度(我画了一条粗红线来标记结束)
  2. 水平滚动视图不水平滚动
  3. ListView 行的宽度不统一(这可以通过背景色结尾看出)(详见下面的getView代码)

    private static final int listRowLayout = android.R.layout.activity_list_item;
    private Map<String, Integer> mColors = new HashMap<String, Integer>();
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // No logs here to keep ListView performance good
        ViewHolder holder;
        int color;
    
        if( convertView == null ) {
            convertView = mInflater.inflate(listRowLayout, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(android.R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String data = mData.get(position);
    
        // A compiled regex is faster than String.Contains()
        Matcher m = ViewHolder.regex.matcher(data);
        if( m.find() ) {
            color = mColors.get(m.group(1));
        } else {
            color = mColors.get("V");
        }
    
        holder.text.setText(data);
        holder.text.setBackgroundColor(color);
        return convertView;
    }
    
    private static class ViewHolder {
        TextView text;
        static Pattern regex = Pattern.compile(" ([VIDWEF])/");
    }
    

    }

【问题讨论】:

  • 你的 listview 宽度需要是 match_parent 而不是 wrap_content
  • @Matthew 经过测试,它确实解决了问题 #1 和 #2,问题 #3 仍然存在。请发表您的评论作为答案,如果您对问题 3 解决方案有见解,欢迎您解释。
  • 奇怪的是,Eclipse 中的 lint 显示警告,对于 ListView,android:layout_width 应该是 wrap_content
  • 对于行,我将使用一个适配器,它允许您自定义行的样式及其高度。这是一个非常基本的示例:mkyong.com/android/android-listview-example
  • @Matthew 这次不走运。我尝试了自定义布局,但背景颜色仍然只延伸到 TextView 中的文本。

标签: android android-layout listview horizontal-scrolling


【解决方案1】:

我在尝试显示日志文件时遇到了完全相同的问题。我有一个专门的活动来显示日志文件:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_log);

    // Read in lines from the log file
    File clientLogFile = new File(LOG_FILE);
    ArrayList<String> lines = new ArrayList<String>();
    try
    {
        Scanner scanner = new Scanner((Readable) new BufferedReader(new FileReader(clientLogFile)));

        try
        {
            while(scanner.hasNextLine())
            {
                lines.add(scanner.nextLine());
            }
        }
        finally
        {
            scanner.close();
        }
    }
    catch (FileNotFoundException e)
    {
        lines.add("No log file");
    }

    // Create a simple adaptor that wraps the lines for the ListView
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,lines);

    // Create a ListView dynamically to overide onMeasure()
    final ListView listView = new ListView(this)
    {
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            // Override onMeasure so we can set the width of the view to the widest line in the log file
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

            // Find maximum width of item in list and set scroll width equal to that
            int maxWidth = 0;
            for(int i=0; i<getAdapter().getCount(); i++)
            {
                View listItem = getAdapter().getView(i, null, this);
                listItem.measure(0, 0);
                int width = listItem.getMeasuredWidth();
                if(width > maxWidth)
                {
                    maxWidth = width;
                }
            }

            // Set width of measured dimension
            setMeasuredDimension(maxWidth, getMeasuredHeight());
        }
    };

    // Add to scroll view
    HorizontalScrollView horizontalScrollView = (HorizontalScrollView)findViewById(R.id.logScrollView);
    horizontalScrollView.addView(listView);

    // Set adaptor
    listView.setAdapter(adapter);

    // Enable fast scroll
    listView.setFastScrollEnabled(true);

    // Scroll to end
    listView.post(new Runnable(){
        public void run() {
            listView.setSelection(listView.getCount() - 1);
        }});
}

onCreate 方法读取日志文件,然后将 ListView 动态添加到覆盖 onMeasure() 的 Horizo​​ntalScrollView。 onMeasure() 代码确定适配器中视图的最大宽度,并将 ListView 宽度设置为该宽度。

因此,我的 activity_view_log.xml 布局文件非常简单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/logScrollView">
    </HorizontalScrollView>
</RelativeLayout>

为了对 ListView 中的行进行更细粒度的控制,我在 list_item.xml 中为我的适配器提供了我自己的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:textAppearance="?android:attr/textAppearanceListItemSmall"
          android:inputType="text|none"
    />

在 onCreate() 结束时,我启用快速滚动并滚动到日志文件中的行尾。

【讨论】:

    【解决方案2】:

    我可能会扭转你正在做的事情。创建一个 ListView 并使 listview 中的每个项目都可以水平滚动。这样,项目仅在需要时滚动,并且不会滚动整个屏幕。您可以完全控制每个列表项的尺寸。为此,请使用 cmets 中提到的自定义列表视图适配器。您的问题也可能重复:Android horizontal scroll list

    【讨论】:

    • 没办法,我想让整个屏幕水平和垂直滚动为一个表格,而不是每一行独立滚动。
    【解决方案3】:

    为了解决这 3 个问题,我必须让所有组件(水平滚动视图、列表视图及其项目)都具有“fill_parent”宽度(我认为它与“match_parent”相同)。此外,我重写了列表视图的onMeasure(...) 以计算其项目的最大宽度并通过setMeasuredDimension(...) 设置它。这将通过它最宽的项目来衡量视图,而不是像现在实现的那样通过它的第一个来衡量。

    【讨论】:

    【解决方案4】:

    这是我找到的解决方案。
    万恶之源 :-) 是 ListView 并非旨在有效处理不同长度的行。要确定 ListView 的宽度,而不是查看所有行,只取 3 行作为平均
    因此,如果这 3 行偶然是短行,那么对于较长的行,宽度将被剪裁,这就解释了我遇到的问题。

    为了绕过这个,我计算了所有数据的最大行长度,并用空格填充了较短的行,它解决了我在问题中描述的所有 3 个问题。

    填充代码(在getView()内部执行)

    holder.text.setText(String.format("%1$-" + mLen + "s", data));
    

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      相关资源
      最近更新 更多