【问题标题】:Android UI programmatically GridLayout positionAndroid UI 以编程方式 GridLayout 位置
【发布时间】:2014-04-22 14:59:48
【问题描述】:

我正在尝试实现下面的布局,其想法是对动态 UI 进行编程:

https://www.dropbox.com/s/stlirgv7coqf0eu/mockup1.png.

我用于应用程序的JSON如下:

{
"name": "Formulare",
"url": "http://server.save-data.de/13467",
"rows": 5,
"cols": 3,
"formItems": [
    {
        "type": "button",
        "value": "Abschicken",
        "action": "send",
        "posX": 2,
        "posY": 4,
        "width": 2,
        "height": 2
    },
    {
        "type": "label",
        "value": "Vorname",
        "posX": 1,
        "posY": 2,
        "width": 2,
        "height": 2
    },
    {
        "type": "label",
        "value": "Nachname",
        "posX": 1,
        "posY": 3,
        "width": 2,
        "height": 2
    },
    {
        "type": "inputFieldText",
        "value": "Max",
        "validation": "50",
        "placeholder": "Vorname",
        "name": "firstname",
        "localStorage": true,
        "mandatory": true,
        "posX": 2,
        "posY": 2,
        "width": 2,
        "height": 3
    },
    {
        "type": "inputFieldText",
        "value": "Mustermann",
        "validation": "50",
        "placeholder": "Nachname",
        "name": "lastname",
        "localStorage": true,
        "mandatory": true,
        "posX": 2,
        "posY": 3,
        "width": 2,
        "height": 3
    }        
]}

我使用模拟 http://www.mocky.io/v2/5316e43f2b7484ac02382f7c 作为 JSON-Data 和我从 FasterXML Jackson 中选择的 JSON-Parser -> (github.com/FasterXML/jackson)

实现 JSON 到 Java 对象的工作!问题是,我怎样才能定位对象? 我猜 GridLayout 适合我的需求,但我不知道没有 XML 是如何实现的。

我只能在列表中显示对象,这里是代码:

ScrollView scrollView = (ScrollView) findViewById(R.id.formContainer);

GridLayout layout = new GridLayout(getApplicationContext());
layout.setOrientation(GridLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));

List<FormItem> formItems = form.getFormItems();
if (formItems != null) {
    for (FormItem formItem : formItems) {
        layout.addView(formItem.getViewObject(this));
    }
}
scrollView.addView(layout);

【问题讨论】:

  • 您可以设计一个布局项目,该布局项目在水平方向的线性布局中具有 textview/edittext 、复选框和 radiogroup。 \n 为列表中的每个新条目填充此布局
  • @BlackBeard 你能给我看一个LinearLayout的示例代码吗?你是什么意思?
  • 您遇到的错误是什么?你的getViewObject()方法有什么用??
  • 项目实际上显示在列表中,因为 List formItems = form.getFormItems(); -> dropbox.com/s/6jtnom1zyoetqqs/… 但我想通过在 JSON-Data 中定义来显示项目的位置。目标是以这种形式显示 UI:-> dropbox.com/s/xzgwfdu80g2lrwg/…
  • getViewObject() 方法进入工厂类并获取 JSON 数据。这是按钮的示例代码: public View getViewObject(DynamicForm form){ Button dfbutton = new Button(form); dfbutton.setText(this.getValue()); dfbutton.setWidth(this.getWidth()); dfbutton.setHeight(this.getHeight()); dfbutton.setX(this.getPosX()); dfbutton.setY(this.getPosY());返回df按钮; }

标签: java android json jackson android-gridlayout


【解决方案1】:

你可以做的是在布局 XML 中定义 gridview

<GridView
        android:drawSelectorOnTop="true"
        android:horizontalSpacing="5dp"
        android:id="@+id/gridview"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp" />

然后创建一个网格item.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:hapticFeedbackEnabled="true"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:soundEffectsEnabled="true" >

<com.rizem.cbehindcode.grid.SquareImageView
    android:id="@+id/picture"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scaleType="centerCrop" />

<TextView
    android:background="#55000000"
    android:gravity="center"
    android:hapticFeedbackEnabled="true"
    android:id="@+id/titeText"
    android:layout_gravity="bottom"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:paddingBottom="15dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:soundEffectsEnabled="true"
    android:textAppearance="?android:attr/textAppearanceSearchResultTitle" />

现在通过扩展 BaseAdapter 类来创建自定义适配器。

 public class IndexAdapter2 extends BaseAdapter {

        private List<Item> items = new ArrayList<Item>();
        private LayoutInflater inflater;
        private Context context;
        private boolean selected = false;

        public IndexAdapter2(Context context) {
            inflater = LayoutInflater.from(context);
            this.context = context;
                //TODO :Parse your json and add item according to it
                // in my case i am adding only textview and image view you can add other
                //Controls also .

            items.add(new Item("Chapter 1 ", R.drawable.background1));
            items.add(new Item("Chapter 2 ", R.drawable.background2));
            items.add(new Item("Chapter 3 ", R.drawable.background3));
            items.add(new Item("Chapter 4 ", R.drawable.background4));
            items.add(new Item("Chapter 5", R.drawable.background5));
            items.add(new Item("Chapter 6", R.drawable.background6));
            items.add(new Item("Chapter 7", R.drawable.background7));
            items.add(new Item("Chapter 8", R.drawable.background8));
            items.add(new Item("Chapter 9", R.drawable.background9));
            items.add(new Item("Chapter 10", R.drawable.background10));

        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i) {
            return items.get(i);
        }

        @Override
        public long getItemId(int i) {
            return items.get(i).drawableId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = view;
            ImageView picture;
            TextView name;

            if (v == null) {
                         //Inflate your custom gird item list. and set the controls data
                v = inflater.inflate(R.layout.grid_item, viewGroup, false);
                v.setTag(R.id.picture, v.findViewById(R.id.picture));
                v.setTag(R.id.titeText, v.findViewById(R.id.titeText));

            }

            picture = (ImageView) v.getTag(R.id.picture);
            name = (TextView) v.getTag(R.id.titeText);
            name.setTypeface(Typeface.DEFAULT_BOLD);
            name.setTextColor(Color.WHITE);
            name.setTextSize(15);

            Item item = (Item) getItem(i);

            picture.setImageResource(item.drawableId);
            name.setText(item.name);
            v.setTag(i);
            return v;
        }

        private class Item {
            final String name;
            final int drawableId;

            Item(String name, int drawableId) {
                this.name = name;
                this.drawableId = drawableId;
            }
        }
    }

在我的情况下,我并排显示 2 个带有文本框的图像视图,因此我创建了一个自定义图像视图类来调整网格项目 list.xml 中使用的图像尺寸

public class SquareImageView extends ImageView {

public SquareImageView(Context context) {
    super(context);

}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}

}

现在在你的活动中

//Get the grid layout object.
GridView app_gridView = (GridView) findViewById(R.id.gridview);

// Instance of IndexAdapter Class.
app_gridView.setAdapter(new IndexAdapter2(this));

【讨论】:

  • 感谢@RizN81 的快速回答!我将对其进行测试,但问题是,我不想使用 XML。我想以编程方式创建动态 UI。这意味着我将 JSON-Data 解析为 JSON-Object 并使用来自 JSON 的信息显示 Android-UI。请看我上面制作的cmets。所以表格应该是这样的。
  • 那么我认为你应该对 xml 部分做一些尝试和错误。我应该建议您通过扩展它来创建自定义网格视图类,然后创建自定义网格项类。
【解决方案2】:

将其用作 json 中每个条目的项目,但控制哪个视图可见

`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="left"
        android:textAlignment="gravity" />

    <EditText
        android:id="@+id/input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:hint="data"
        android:textAlignment="gravity" />

    <LinearLayout
        android:id="@+id/checkboxGrp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <RadioGroup
        android:id="@+id/radioGrp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical" >

        <!-- add as many radio buttons as possible -->

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    </RadioGroup>

</LinearLayout>`

将此视图添加到您的滚动视图容器中

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(layout_resource, null);

EX:从快照的第 2 行开始,只有输入字段可见。所以让其他人走了

findViewById(checkboxGrp).setVisibility(View.GONE) findViewById(radioGrp).setVisibility(View.GONE)

禁用这些组的可见性

这可能不是一个完整的布局,只是玩弄直到你做对了 如果要显示的项目很多,我的建议是使用列表视图而不是滚动视图

【讨论】:

    猜你喜欢
    • 2016-12-09
    • 2017-04-07
    • 1970-01-01
    • 2014-10-09
    • 2016-11-13
    • 2011-03-14
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多