【问题标题】:Android : Setting text and multiple images on top of image not working properly.Android:在图像顶部设置文本和多个图像无法正常工作。
【发布时间】:2015-08-12 08:30:28
【问题描述】:

我正在开发一个 Android 应用程序,其中我有一个以静态方式设置的部分图像。现在,为了尝试,我在框架布局中使用 GridView。但是当我这样做时,我无法正确看到字母,因为我认为它们已经超出了视图屏幕并且该部分的文本不再可见。我不知道我做错了什么。任何帮助都会很好。

activity_group_section.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <FrameLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">
        <ImageView
            android:id="@+id/sectionimage"
            android:layout_width="386dp"
            android:layout_height="200dp"
            android:scaleType="fitXY"
            android:padding="5dp"
            android:src="@drawable/sectionbackground"
            />

        <TextView
            android:id="@+id/sectionname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textView"
            android:visibility="visible"
            android:gravity="center"
            android:layout_gravity="center_horizontal" />

        <GridView
            android:id="@+id/grid_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3"/>

    </FrameLayout>
</RelativeLayout>

SectionLazyAdapter.java:

public class SectionLazyAdapter extends BaseAdapter{

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;


    GridView gridView;

    static final String[] numbers = new String[] {
            "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};


    public SectionLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.activity_group_section, null);

        TextView sectionName = (TextView)vi.findViewById(R.id.sectionname); // title
        GridView gridView = (GridView) vi.findViewById(R.id.grid_view);
        HashMap<String, String> sectionList = new HashMap<String, String>();
        sectionList = data.get(position);

        sectionName.setText(sectionList.get(GroupSectionActivity.msectionname));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
                android.R.layout.simple_list_item_1, numbers);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

            }
        });

         return vi;
    }
}

这是它外观的屏幕截图,我想查看图像中的字母和图像下方的部分名称,但这不是正在发生的事情,图像也在外面。

我认为这是 XML 文件中配置错误的问题。任何帮助都会很好。

【问题讨论】:

  • 您不应再使用fill_parent,它已被弃用。使用match_parent

标签: java android android-framelayout


【解决方案1】:

发生这种情况的原因是您试图将 GridView 放入 ListView。
我看到你这样做是因为你想要一个分段的 GridView,我建议只使用一个这样做的库,比如this one。谷歌“gridview with section”如果你对它不满意,你会发现很多其他库和 SO 问题(我使用了链接中的那个并且很满意)。

现在关于您的方法不起作用的原因。有多种原因会导致您难以采用这种方法。

  • ListView 不能很好地处理其他可滚动组件(无论是作为父组件还是作为子组件)。
  • ListViews 为孩子们提供他们想要的所有高度,就像 ScrollView 对其孩子所做的那样。 GridView 还没有为此做好准备,您需要为其强制设置一定的高度。
  • infinite 视图中拥有 AdapterView 会使 View 呈现 all 它的子视图。这使得回收观点的整个观点毫无意义

您可以尝试并继续走这条路,但恐怕 Android 会对您不利。

【讨论】:

  • 我将开始研究它。你能给我一个例子,我如何用库实现同样的目标,我是 Android 新手,尤其是 UI 编程,这件事让我发疯了。
  • 我看到他们提供了example here。我还看到我给你的图书馆已经停产了。它已被使用 RecyclerView 的更好的 here 所取代 - 这个也是 has examples。我不会编辑我的答案,因为原始库是 GridView 的直接替代品,但我强烈建议您使用 newer one
【解决方案2】:

框架布局没用,你应该去掉它。

将此行添加到部分名称 textview:

android:layout_below="@id/sectionimage"

PS:不建议将可滚动视图放在另一个可滚动视图中,因为滚动会遇到问题。

【讨论】:

  • 我应该使用哪种布局,以便 gridview 的内容显示在 sectionimage 中?
  • NestedScrollView 怎么样?
猜你喜欢
  • 2020-11-04
  • 2015-05-27
  • 2012-07-04
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
相关资源
最近更新 更多