gpt4 book ai didi

java - 在 Android 中使用适配器向 GridView 添加更多元素

转载 作者:行者123 更新时间:2023-12-01 12:30:37 25 4
gpt4 key购买 nike

这是所需的输出:

/image/H3xgw.jpg

基本上,我使用 GridView 生成所有这 18 个按钮,并且尝试在每个按钮顶部添加一些 TextView,并在最后一个按钮的底部添加 2 个 TextView。

这是我的适配器实现:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

/**
* Created by pop on 9/15/2014.
*/
public class CustomGridAdapter extends BaseAdapter {

private Context _context;
private String[] _items;
LayoutInflater _inflater;

public CustomGridAdapter(Context context, String[] items) {
this._context = context;
this._items = items;
_inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return _items.length;
}

@Override
public Object getItem(int position) {
return _items[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = _inflater.inflate(R.layout.screen_gridcell_time, null);
}
Button button = (Button) convertView.findViewById(R.id.calendar_day_gridcell);
button.setText(_items[position]);

return convertView;
}
}

我的网格单元:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent"
android:padding="4dp">

<ToggleButton
android:id="@+id/calendar_day_gridcell"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/wampWhite"
android:background="@drawable/time_input_button_selector"
android:layout_marginLeft="2dp"
android:textOff=""
android:textOn="\u2713"
/>

</RelativeLayout>

以及使用这两个的Java文件:

public class TimeActivity extends Activity  {

private GridView gridView;
private final String[] items = new String[] {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
setFonts();

gridView = (GridView) this.findViewById(R.id.timeInputView);
CustomGridAdapter adapter = new CustomGridAdapter(TimeActivity.this, items);
gridView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.time, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

private void setFonts() {
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Medium.ttf");
TextView dateOutput = (TextView) findViewById(R.id.dateOutput);
dateOutput.setTypeface(tf);
}
}

我尝试在网格单元中添加一个 TextView,然后给它 android:layout_above(id ofbuttons) 但它就在按钮内部,在按钮的顶部。有什么建议么?干杯!

最佳答案

这是让您的relativelayout看起来像您想要的那样的一种方法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent"
android:padding="4dp">

<TextView
android:id="@+id/gridcell_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="6:00" />

<ToggleButton
android:id="@+id/calendar_day_gridcell"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_below="@id/gridcell_time"
android:layout_gravity="center"
android:textColor="@color/wampWhite"
android:background="@drawable/time_input_button_selector"
android:layout_marginLeft="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textOff=""
android:textOn="\u2713" />

</RelativeLayout>

我所做的就是在 ToggleButton 之前添加 TextView 并将 TextView 的 layout_centerHorizo​​ntal 属性设置为 true。我还将 ToggleButton 的 below 属性更改为 TextView 的 id(在本例中我将其称为 gridcell_time)。

关于java - 在 Android 中使用适配器向 GridView 添加更多元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com