gpt4 book ai didi

android - 创建一个类似于 Android Lollipop 的 Speed Dial 的 View

转载 作者:行者123 更新时间:2023-11-29 17:35:12 25 4
gpt4 key购买 nike

我如何创建一个类似于 Android Lollipop 中引入的快速拨号的 View ,当我们按下电话图标时可以找到它?图像如下所示,其中隐藏了联系人图像和联系人姓名。

Want to create something link this.

编辑:到现在为止,我已经得到了这么多,并且正在寻找更多可以改进的地方。本示例使用 Fragment 中的 GridView。

public class XYZFragment extends Fragment {

private View rootView;

GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;

public XYZFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.fragment_XYZ, container, false);

//set grid view item
Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_home);
Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_communities);

gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"House"));
gridArray.add(new Item(userIcon,"Friend"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"Personal"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"Building"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"xyz"));


gridView = (GridView) rootView.findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(getActivity(), R.layout.row_grid, gridArray);
gridView.setAdapter(customGridAdapter);

return rootView;
}
}

行网格.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >

<ImageView
android:id="@+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_home" >
</ImageView>

<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>

</LinearLayout>

fragment _xyz.xml

<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="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>

</RelativeLayout>

项目.java

public class Item {
Bitmap image;
String title;

public Item(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

CustomGridViewAdapter.java

public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();

public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;

if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}

Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}

static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}

最佳答案

更改 GridView

<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:numColumns="auto_fit"
android:background="#abcdef"
android:stretchMode="columnWidth" >

然后在您的 GridAdapter 中获取行 xml 的父 ID 并在运行时设置背景颜色。

关于android - 创建一个类似于 Android Lollipop 的 Speed Dial 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30857770/

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