我想知道是否有人能够对动态创建控件(inflate vs instantiate)的最佳实践有所了解.
膨胀:
TextView styledText = (TextView)inflater.inflate(R.layout.styledTextView);
Run Code Online (Sandbox Code Playgroud)
实例:
TextView styledText = new TextView(mContext);
styledText.setTextAppearance(R.style.StyledTextStyle);
Run Code Online (Sandbox Code Playgroud)
正在创建的对象可以包含膨胀的XML文件中的属性,也可以包含在随后添加到实例化对象的样式定义中.(假设此样式包括宽度,背景,文本颜色等).
无法对每种方法进行任何时间/内存测试,想知道是否有人知道哪种方法最快/最有效.
我最近开始使用genymotion而不是经典的Android虚拟设备,但我有一些问题..当我尝试运行我的应用程序时,我收到此错误.
Can't convert to dimension: type=0x1
Run Code Online (Sandbox Code Playgroud)
我来自LayoutInflater ..当我在Genymotion中运行时,它说有一些布局参数有不好的类型..下面是android studio的两个截图.第一个是在Nexus 4上运行应用程序时使用的,第二个是来自Genymotion.


两者都应该运行Jelly Bean,唯一的区别是Genymotion是在API 16上,而Nexus 4在4.2.2上运行最新,因此API 17 ..
问题来自我的自定义列表视图适配器 - 来自其getView方法,因此我认为它必须与其中一个布局相关.(我有两种不同类型的列表项)
list_heading.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/listViewHeaderText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="10dp"
android:paddingBottom="3dp"
android:text="Nacionále"
android:textAppearance="?android:attr/listSeparatorTextViewStyle"
android:textColor="@color/main_cvut"/>
<RelativeLayout
android:id="@+id/listViewHeaderLine"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@color/main_cvut"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_layout"
android:clickable="true"
android:focusable="true"
android:paddingTop="?android:attr/listPreferredItemPaddingStart"
android:minHeight="?android:attr/listPreferredItemHeight">
<TextView
android:id="@+id/itemTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/itemDescription"
android:textColor="@android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="description"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="?android:attr/listPreferredItemPaddingEnd"
android:paddingBottom="?android:attr/listPreferredItemPaddingRight"
android:layout_below="@+id/itemTitle"/>
<ImageView …Run Code Online (Sandbox Code Playgroud) 我创建了要从按钮单击动态添加到布局的视图,但是当我旋转设备格局时,视图消失.有人可以看看我的代码并解释如何阻止这种情况发生.
这是代码:
public class TestContainerActivity extends Activity implements OnClickListener {
LinearLayout containerLayout;
Button testButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_container);
testButton = (Button)findViewById(R.id.testContainerButton1);
testButton.setOnClickListener(this);
containerLayout = (LinearLayout)findViewById(R.id.testContainerLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_container, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==testButton){
createNewLayout();
}
}
public void createNewLayout(){
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View …Run Code Online (Sandbox Code Playgroud) 我正在使用片段和..
我的代码中遇到了一个我无法找到的问题.
logcat指向这段代码,在我的一个片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
Run Code Online (Sandbox Code Playgroud)
我的主要类(FragmentActivity):
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class Fragments extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments);
MainActivity fragment = new MainActivity();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_place, fragment);
transaction.commit();
}
public void onSelectFragment(View view) {
Fragment newFragment;
if (view == findViewById(R.id.add)) {
newFragment = new Add();
} else if (view == …Run Code Online (Sandbox Code Playgroud) android class android-fragments layout-inflater android-fragmentactivity
我必须显示具有不同类型视图的列表。所以我必须定义一个带有适配器的 ListView,我必须在其中膨胀多个视图。我已经完成了给出的示例,但问题是我的列表不对称,就像在 4 个项目之后每次重复标题的示例一样。所以我面临着重用物品的问题getView()
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_1:
convertView = mInflater.inflate(R.layout.item1, null);
.......
break;
case TYPE_2:
convertView = mInflater.inflate(R.layout.item2, null);
.......
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
............
............
}
Run Code Online (Sandbox Code Playgroud)
现在,如果 convertView 不为空,但之前使用的项目类型不同,则布局将不匹配。这段代码将如何处理这个问题?
我创建了一个使用此hdodenhof CircleImageview库的应用程序。我的应用程序可在Android 7和7.1上正常运行,但在棉花糖/ Android 6等较低版本中崩溃。如何解决此问题?我已经阅读了他的GitHub帖子,但是由于人们要求从.xml中删除属性,所以它不能解决我的问题,但是我没有使用这些属性。
这是CircleImageView的简单实现:
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/civ_requests"
android:layout_width="80sp"
android:layout_height="80sp"
android:layout_margin="10sp"
android:src="@drawable/profilesvg" />
Run Code Online (Sandbox Code Playgroud)
这是错误:
FATAL EXCEPTION: main
Process: com.milind.locatemymate, PID: 4288
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class de.hdodenhof.circleimageview.CircleImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.milind.locatemymate.Fragment.Requests$1.onCreateViewHolder(Requests.java:152)
at com.milind.locatemymate.Fragment.Requests$1.onCreateViewHolder(Requests.java:103)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1632)
at …Run Code Online (Sandbox Code Playgroud) android android-layout android-xml layout-inflater android-6.0-marshmallow
这是我的类DownloadableProjectsFromWebAdapter的getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
DownloadableProjectsFromWebHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DownloadableProjectsFromWebHolder();
holder.txtPid = (TextView)row.findViewById(R.id.tvAllProjectsPid);
holder.txtName = (TextView)row.findViewById(R.id.tvAllProjectsName);
holder.txtShareable = (TextView)row.findViewById(R.id.tvAllProjectsShareable);
row.setTag(holder);
}
else
{
holder = (DownloadableProjectsFromWebHolder)row.getTag();
}
String share_text;
AllProjectListInfo li = data.get(position);
holder.txtPid.setText("ID: " + Integer.toString(li.getId()));
holder.txtName.setText(li.getName());
if (li.getShareable()){
share_text = "Public Project";
} else {
share_text = "Private Project";
}
holder.txtShareable.setText(share_text);
return …Run Code Online (Sandbox Code Playgroud) 点击一个按钮,我试图膨胀的EditText上方RecyclerView。但不是将 EditText 添加到布局的顶部,从而将 RecyclerView 向下推,而是将它简单地放置在 RecyclerView 的顶部,像这样覆盖它:

您可以看到“新文本”覆盖了 RecyclerView 的第一项。如何让它在 RecyclerView 上方膨胀,并将 RecyclerView 向下推,以容纳新添加的 EditText?
这是我将 EditText 添加到的 FrameLayout 的 XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_my_frame_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<com.melnykov.fab.FloatingActionButton
android:id="@+id/button_floating_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_action_new"
fab:fab_colorNormal="@color/primary_dark"
fab:fab_colorPressed="@color/primary" />
Run Code Online (Sandbox Code Playgroud)
这是我的 FloatingActionButton 的 onClick 方法,我希望在 RecyclerView 上方添加 EditText:
public void onClick(View v) {
ViewGroup parent = (ViewGroup) rootView.findViewById(R.id.fragment_my_frame_layout);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.add_keyword_edittext, parent, true);
}
Run Code Online (Sandbox Code Playgroud)
这是我添加的 EditText 的 XML:
<?xml version="1.0" encoding="utf-8"?> …Run Code Online (Sandbox Code Playgroud) android layout-inflater android-recyclerview android-framelayout
在三星 Android 13 设备中出现奇怪的崩溃,但这在其他设备上运行良好。最近,我们在 Firebase 崩溃分析中遇到了一起崩溃,下面是崩溃报告。应用程序的目标 API 级别为 33,到目前为止仅在三星 Android 13 设备中报告崩溃。
编辑文本.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/login_input1_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/input_field_text_color"
android:theme="@style/login_floating_text_style">
<com.mobileaware.unified.ui.controls.common.MaEditText
android:id="@+id/login_input1"
style="@style/input_field_validation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/login_input2_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/input_field_text_color"
android:theme="@style/login_floating_text_style">
<com.mobileaware.unified.ui.controls.common.MaEditText
android:id="@+id/login_input2"
style="@style/input_field_validation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/login_input3_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/input_field_text_color"
android:theme="@style/login_floating_text_style">
<com.mobileaware.unified.ui.controls.common.MaEditText
android:id="@+id/login_input3"
style="@style/input_field_validation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪 :
Fatal Exception: android.view.InflateException
Binary XML file line #21 `com.xyz.abc:layout/edit_texts: Attempt to …Run Code Online (Sandbox Code Playgroud) crash android android-layout layout-inflater inflate-exception
android ×10
layout-inflater ×10
android-xml ×1
baseadapter ×1
class ×1
crash ×1
dynamic ×1
getview ×1
listview ×1
orientation ×1
performance ×1
view ×1