我需要在布局中创建超过80个数字。目前我正在收到“有超过80次观看,不利于表现”的消息,我想找到另一种方法来做到这一点。我已经研究过GridView,但我似乎无法找到将背景应用于每个数字的方法,如下面链接中的图片(我无法上传图片)。
<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" >
<LinearLayout
android:id="@+id/linearlayout_button"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true" >
<Button
android:id="@+id/button_play"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_play"
android:enabled="false"
android:baselineAligned="false" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/linearlayout_button" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:id="@+id/textview_favorite_numbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@android:color/darker_gray"
android:text="@string/favorite_numbers"
android:gravity="center_horizontal" />
<LinearLayout
android:id="@+id/linearlayout_numbers1to10"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/textView_number1"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_1"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number2"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_2"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number3"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_3"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number4"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_4"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number5"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_5"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number6"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_6"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number7"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_7"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number8"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_8"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number9"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_9"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number10"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_10"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<!-- 11 to 20 -->
<LinearLayout
android:id="@+id/linearlayout_numbers11to20"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/textView_number11"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_11"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number12"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_12"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number13"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_13"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number14"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_14"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number15"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_15"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number16"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_16"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number17"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_17"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number18"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_18"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number19"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_19"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number20"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_20"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<!-- 21 to 30 -->
<LinearLayout
android:id="@+id/linearlayout_numbers21to30"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/textView_number21"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_21"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number22"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_22"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number23"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_23"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number24"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_24"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number25"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_25"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number26"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_26"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number27"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_27"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number28"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_28"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number29"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_29"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number30"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_30"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<!-- 31 to 40 -->
<LinearLayout
android:id="@+id/linearlayout_numbers31to39"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/textView_number31"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_31"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number32"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_32"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number33"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_33"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number34"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_34"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number35"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_35"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number36"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_36"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number37"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_37"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number38"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_38"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number39"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_39"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number40"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_40"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
<!-- 41 to 47 -->
<LinearLayout
android:id="@+id/linearlayout_numbers41to47"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" >
<TextView
android:id="@+id/textView_number41"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_41"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number42"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_42"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number43"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_43"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number44"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_44"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number45"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_45"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number46"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_46"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number47"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/number_47"
android:textSize="18sp"
android:textStyle="bold"
android:background="@drawable/number_background"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:id="@+id/textView_number48"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1" />
<TextView
android:id="@+id/textView_number49"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1" />
<TextView
android:id="@+id/textView_number50"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1" />
</LinearLayout>
...
</LinearLayout>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
我使用GridView进行了工作!
以下是我的代码:
public class Fragment1 extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public Fragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_1, container, false);
GridView gridView = (GridView) rootView.findViewById(R.id.gridview_numbers);
gridView.setAdapter(new TextViewAdapter(getActivity()));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
public class TextViewAdapter extends BaseAdapter {
private Context mContext;
public TextViewAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if(convertView == null)
{
textView = new TextView(mContext);
textView.setLayoutParams(new GridView.LayoutParams(65, 85));
textView.setTextSize(18.0f);
textView.setGravity(0x11);
}
else
{
textView = (TextView) convertView;
}
textView.setBackgroundResource(mThumbIds[position]);
textView.setText(Integer.toString(position + 1));
return textView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.number_deselect, R.drawable.number_deselect, R.drawable.number_deselect,
R.drawable.number_deselect, R.drawable.number_deselect, R.drawable.number_deselect,
R.drawable.number_deselect, R.drawable.number_deselect, R.drawable.number_deselect
};
}
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview_numbers"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="33dp"
android:numColumns="auto_fit"
android:verticalSpacing="8dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center" />