我正在开发一个 Android 项目,并且正在创建一个自定义列表视图。列表视图包含多个文本视图,我正在为我的文本视图创建一个样式,因此我不必一遍又一遍地重复相同的样式。
下面是我的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/txtFirstOccurred"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/CrashGroup"/>
<TextView android:id="@+id/txtLastOccurred"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/CrashGroup"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
以下是我的文本视图的样式
<style name="CrashGroup">
<item name="android:singleLine">false</item>
<item name="android:textSize">@android:style/TextAppearance.Small</item>
<item name="android:gravity">center_vertical</item>
</style>
Run Code Online (Sandbox Code Playgroud)
下面是我如何扩展 XML 以显示在列表视图上。
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
try
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.android_crash_group_layout, null);
TextView txtFirstOccurred = (TextView)convertView.findViewById(R.id.txtFirstOccurred);
txtFirstOccurred.setText(getItem(position).getFirstOccurred());
TextView txtLastOccurred = (TextView)convertView.findViewById(R.id.txtLastOccurred);
txtLastOccurred.setText(getItem(position).getLastOccurred());
return convertView;
}
catch (Exception ex)
{
Log.e(TAG, ex.toString());
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误是:
进程:com.BoardiesITSolutions.CritiMonApp,PID:1451 java.lang.NullPointerException:尝试在 …
android listview android-listview layout-inflater inflate-exception
我为我的活动设置了一个巨大的布局,我使用setContentView(). 在该布局中,我有一个TableLayout(tableLayout在我的活动中命名)要填充。该 TableLayout 的行是布局文件中的 customViews (我们称之为该文件tablerow_layout.xml)。在这个布局中,我有一些 TextViews 和 ImageView。我想要做的是在创建行时以编程方式更改 TextView 的内容。这是我的代码:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView) newRow. findViewById(R.id.tablerow_name);
name.setText("THIS LINE");
tableLayout.addView(newRow);
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试更改 TextView 的内容时,我的应用程序崩溃了。我知道我可以使用 ListView 来做到这一点,但我通常只有少量行,并且为 ListView 创建另一个适配器的开销非常大。
作为 @???????K 建议我也尝试过 newRow.findViewById(...) 而不是 findViewById(...) 没有任何区别
我目前使用该DataBindingUtil.inflate(inflater, layoutId, parent, attachToParent)方法膨胀我的大部分布局。
但是我看到他们是一个AsyncLayoutInflater因为Support Library revision 24它允许通货膨胀发生在一个单独的线程上。我想在我的应用程序的某些部分使用这种机制,但我不想放弃使用databinding。
DataBindingUtil不包含任何方法,如inflateAsync(). 但它是否计划增加对此的支持?或者他们是将AsyncLayoutInflater和 的使用结合起来的一种方式databinding?
我试图使用AsyncLayoutInflater里面的inflate方法DataBindingUtil但实际上AsyncLayoutInflater不是原始LayoutInflater.
谢谢阅读!
我使用 LayoutInflater 来膨胀自定义布局以生成位图形式的发票和收据,然后将它们发送到打印机或将它们导出为 png 文件,如下所示:
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.layout_invoice, null);
// Populate the view here...
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(paperWidth, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthMeasureSpec, heightMeasureSpec);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
view.layout(0, 0, width, height);
Bitmap reVal = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(reVal);
view.draw(canvas);
Run Code Online (Sandbox Code Playgroud)
现在,这段代码可以完美运行,但它会以设备当前语言扩展视图。有时我需要用其他语言生成发票,有什么方法可以在自定义区域设置中夸大该视图吗?
注意:我尝试在膨胀视图之前更改区域设置并在之后重置它:
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = new Locale("fr");
// Inflate the view
// ...
// Reset the locale to …Run Code Online (Sandbox Code Playgroud) 我有一个CustomView来自 的扩展RelativeLayout。
我CustomView在布局文件中使用它,但Android Studio在设计模式下不显示CustomView. 有什么问题吗?
考拉_appbar.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top"
tools:context=".activity.basic.BaseActivity">
<ImageView
android:id="@+id/appbar_imgLogo"
android:layout_width="@dimen/appBar_iconWidth"
android:layout_height="@dimen/appBar_iconHeight"
android:src="@drawable/logo"
android:layout_marginTop="@dimen/appBar_iconTopMargin"
android:layout_marginLeft="@dimen/appBar_iconLeftMargin"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/appbar_height"
android:orientation="horizontal"
android:layoutDirection="ltr"
>
<koala.android.customer.views.widget.CustomTextView
android:id="@+id/appBar_txtTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|right"
android:text="@string/app_name_persian"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/TextSize_large"
app:tvCustomFont="@string/baseFont"
/>
<koala.android.customer.views.widget.CustomImageView
android:id="@+id/appBar_imgBack"
android:layout_width="@dimen/appbar_height"
android:layout_height="match_parent"
app:srcCompat="@drawable/ic_arrow_right"
app:ivDrawableTint="@color/colorPrimary"
android:padding="@dimen/appBar_backPadding"
/>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
KoalaAppBar.java:
public class KoalaAppBar extends RelativeLayout {
private static final String DEFAULT_TITLE = "default";
private static final int DEFAULT_TITLE_COLOR = ResourcesCompat.getColor(AppController.getContext().getResources(), …Run Code Online (Sandbox Code Playgroud) android custom-view android-custom-view android-layout layout-inflater
I am working with two recyclerview in single screen(For android TV).Each recyclerview have complex layout item.And it's taking time to load.I worked with asynclayoutinflator in activities.
AsyncLayoutInflater inflater = new AsyncLayoutInflater(this);
inflater.inflate(R.layout.main, null, callback);
Run Code Online (Sandbox Code Playgroud)
I want to know whether there is any ways to achieve the same with recyclerview. Problem I am facing is onbindviewholder is getting called before asyncinflation finished.
android asynchronous android-inflate layout-inflater android-recyclerview
我使用基础活动作为另一个活动“RecicpeActivity”的父活动,因为我已经覆盖了基础活动中的 setContentView 方法,以便子活动可以使用它并传递其布局以在框架布局中膨胀。子活动“RecicpeActivity”使用数据绑定来设置其视图。我正在做的是我试图将子活动的布局膨胀到基础活动中的框架布局“容器”中,但根本没有考虑数据绑定,因为我有一个白屏,即使我已经看到了调试时将子活动的布局作为框架布局的子项。
1- 第一个我尝试通过调用 setContentView 来传递子活动的布局,并将传递的布局膨胀到基础活动中的框架布局。
2- 第二个我尝试在基本活动中使用数据绑定,但我认为这无关紧要。
_ChildActivity
public class RecipeActivity extends BaseActivity {
private ActivityRecipeBinding mBinding;
private static final String RECIPE_INTENT_KEY = "recipe key";
private ScrollView mScrollView;
private RecipeViewModel recipeViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recipeViewModel =
ViewModelProviders.of(this).get(RecipeViewModel.class);
mBinding = DataBindingUtil.inflate(
getLayoutInflater(),
R.layout.activity_recipe, null, false);
setContentView(R.layout.activity_recipe);
// This method is implemented in the BaseActivity.
showProgressBar(true);
recipeViewModel.getRecipe().observe(this, new Observer<Recipe>() {
@Override
public void onChanged(Recipe recipe) {
if (recipe != null){
if (recipe.getRecipe_id().equals(
recipeViewModel.getRecipeId())){
mBinding.setRecipe(recipe); …Run Code Online (Sandbox Code Playgroud) 在三星 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
我想知道LayoutParams它将如何运作LayoutInflator.有什么区别:
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, null); //FIRST WAY
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, container,false); //SECOND WAY
Run Code Online (Sandbox Code Playgroud)
因为,这两种方法都给我不同的结果.实际上第二次充气方法给了我两个子布局改变的正确结果,但是第一种方法会给我不同的结果.
这是我的代码:
MainActivity.Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainLayout=(LinearLayout)findViewById(R.id.mainLayout);
LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
for(int i=0;i<10;i++){
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, null); //First WAY
// LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, mainLayout,false); //SECOND WAY
mainLayout.addView(childLayout);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
childitemlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:orientation="vertical"
android:background="#525f67">
<TextView android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"
android:gravity="center"
/>
</LinearLayout> <!-- Both ways gives …Run Code Online (Sandbox Code Playgroud) 如果我android:textSize="?android:attr/textAppearanceLarge"在我的xml中使用我的应用程序崩溃.我不想给出硬编码文本大小,因为我必须为不同的屏幕尺寸创建create 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" >
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/title"
android:textSize="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/saveOnDialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/save"
style="?android:attr/buttonBarButtonStyle"
android:textSize="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/cancelOnDialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/cancel"
style="?android:attr/buttonBarButtonStyle"
android:textSize="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是Log.e输出 -
06-26 11:46:27.439: E/AndroidRuntime(1731): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
06-26 11:46:27.439: E/AndroidRuntime(1731): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-26 11:46:27.439: E/AndroidRuntime(1731): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
06-26 11:46:27.439: E/AndroidRuntime(1731): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
06-26 11:46:27.439: E/AndroidRuntime(1731): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-26 11:46:27.439: E/AndroidRuntime(1731): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-26 11:46:27.439: E/AndroidRuntime(1731): …Run Code Online (Sandbox Code Playgroud) android ×10
layout-inflater ×10
asynchronous ×2
crash ×1
custom-view ×1
data-binding ×1
listview ×1
locale ×1