以编程方式将视图添加到布局是否节省应用程序 Space(兆字节)与手动制作布局?
Does Programmatically Adding Views to Layout Save App Space (megabytes) Versus Manually Making the Layout?
我正在尝试在 Android Studio 中制作一个具有多个可导航页面的信息应用程序。我可能会有大约 40 页左右的信息。我想知道我是否应该为每一页(包含 headers、图像和信息)手动创建一个 activity,或者使用一个 activity 并根据一个字符串。
我决定先尝试只使用一个 activity,它工作正常,但添加新功能(如不同字体以强调单词或标题或添加公式)需要复杂得多的代码。
例如,我会在我的 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 使用了很多与其他用途重叠的字符。物流变得更加复杂。
因此,我想知道以我现在的方式做这件事有什么好处。如果它只是一个较小的应用程序文件大小,那么也许我会手动制作每个页面。感谢您的帮助。
如果您必须在一个视图中添加多个视图 activity,创建自定义视图可能更高效 class。
例如,像这样创建自定义视图 class。
布局:
<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"
/>
Class:
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);
....
}
}
创建此 class 后,您可以在需要将此视图添加到主 activity 布局时创建新的自定义视图实例。
//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);
...
然后您可以轻松地在主布局中添加许多视图。
我正在尝试在 Android Studio 中制作一个具有多个可导航页面的信息应用程序。我可能会有大约 40 页左右的信息。我想知道我是否应该为每一页(包含 headers、图像和信息)手动创建一个 activity,或者使用一个 activity 并根据一个字符串。
我决定先尝试只使用一个 activity,它工作正常,但添加新功能(如不同字体以强调单词或标题或添加公式)需要复杂得多的代码。
例如,我会在我的 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 使用了很多与其他用途重叠的字符。物流变得更加复杂。 因此,我想知道以我现在的方式做这件事有什么好处。如果它只是一个较小的应用程序文件大小,那么也许我会手动制作每个页面。感谢您的帮助。
如果您必须在一个视图中添加多个视图 activity,创建自定义视图可能更高效 class。 例如,像这样创建自定义视图 class。
布局:
<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"
/>
Class:
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);
....
}
}
创建此 class 后,您可以在需要将此视图添加到主 activity 布局时创建新的自定义视图实例。
//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);
...
然后您可以轻松地在主布局中添加许多视图。