android - 以编程方式将视图添加到布局是否会节省应用程序空间(兆字节)与手动制作布局?
问题描述
我正在尝试制作一个在 Android Studio 中具有多个可导航页面的信息应用程序。我可能会有大约 40 页左右的信息。我想知道是否应该为每个页面(带有标题、图像和信息)手动创建一个活动,或者使用一个活动并根据字符串以编程方式添加文本视图和图像视图。
我决定首先尝试只使用一个活动,它工作正常,但添加新功能,如不同的字体以强调单词或标题或添加公式将需要更复杂的代码。
例如,我会在我的 string.xml 文件中有这个。
<string name="page1">
~Introduction~\n
Hi guys!\n
`image`
</string>
然后我会有一个名为 updatePage() 的方法来检查我的标记,例如 ~ 用于标题和 ` 用于图像。
private void updatePage(int pPageNum){
String dayString = "";
pageNum = pPageNum;
mScrollView.fullScroll(ScrollView.FOCUS_UP);
mEditor.putInt("page", pageNum);
mEditor.apply();
//todo add substrings to look for specific image and formula markers
try{
dayString = dayJSON.getString(pPageNum+"");
pageInfoLL.removeAllViews();
int i = 0;
int t = 0;
int nonTextStartIndex = 0;
int nonTextEndIndex = 0;
while(i != -1 || t == 3){
if(dayString.contains("`")){
nonTextStartIndex = dayString.indexOf("`");
String textSubstring = dayString.substring(0, nonTextStartIndex);
if(!textSubstring.equals("")){
addTextViewToLayout(pageInfoLL, textSubstring, 12);
dayString = dayString.substring(nonTextStartIndex + 1);
}
nonTextEndIndex = dayString.indexOf("`");
String imageName = dayString.substring(0, nonTextEndIndex);
Log.e("IMAGERESOURCENAME", imageName);
addImageToLayout(pageInfoLL, imageName);
dayString = dayString.substring(nonTextEndIndex+1);
Log.e("After Image String", dayString);
if(dayString.equals("")){
Log.e("String Finished", "STRING FINISHED");
i = -1;
}
t++;
}else{
addTextViewToLayout(pageInfoLL, dayString, 12);
i = -1;
}
}
}catch (JSONException je){
je.printStackTrace();
makeToast("JSON Error, Page Did Not Update Successfully.");
}catch (NullPointerException ne){
ne.printStackTrace();
makeToast("NULL Error, Page Did Not Update Successfully.");
}
}
正如您所看到的,它消除了很多灵活性。我还想使用 jqMath 添加公式,但是 jqMath 使用了许多与其他用途重叠的字符。物流变得更加复杂。因此,我想知道我现在这样做的好处是什么。如果它只是一个较小的应用程序文件大小,那么也许我会手动制作每个页面。谢谢您的帮助。
解决方案
如果您必须在一个活动中添加许多视图,则创建自定义视图类可能会很有效。例如,像这样创建自定义视图类。
布局:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:textSize="18sp"
/>
<View
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_centerVertical="true"
android:visibility="gone"
/>
班级:
public class CustomView extends View {
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
//inflate above layout to this view
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_color_options, this, true);
//initialize textView and ImageView with each view attributes.
TextView title = (TextView) getChildAt(0);
title.setText(titleText);
....
}
}
创建此类后,您可以在需要将此视图添加到主活动布局时创建新的自定义视图实例。
//create new attribute as you want.
....
CustomView customView = new CustomView(context, attribute);
customView.setId(Integer.parseInt("5"));
customView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(customView);
...
然后,您可以轻松地在主布局中添加许多视图。
推荐阅读
- java - 在sql查询中使用变量作为列
- android - 快捷方式创建不起作用
- asterisk - 在 pjsip max_calls 上调用限制为 32 次调用
- mysql - nodejs中的一些代码让我感到困惑
- html - 阻止动画在 CSS 中自动播放
- c - 使用带有文件扩展名 .r 的自动工具作为标题,而不是 ratfor 而是 C 编程
- python - NLTK 错误:OSError:没有这样的文件或目录
- jquery - 如何使用 jQuery 对动态数组进行排序?
- r - 将 auto.arima() 映射到子集时间序列列表
- python - 查找元素值是唯一的,并且列表中的所有元素都相同