layout-inflater 相关问题

layout-inflater标记引用Android LayoutInflater类,该类用于从xml布局文件构建视图层次结构。

我可以在其他功能中使用Canvas元素来显示在视图上吗

我创建了一个布局,其中包含一些视图,还包含一个 LinearLayout 来绘制图形 main_page_layout.xml 我创建了一个布局,其中包含一些视图,还包含一个用于绘制图形的 LinearLayout main_page_layout.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bgcolor" android:id="@+id/main_page_layout" > .... <LinearLayout android:id="@+id/graphicView" android:layout_width="match_parent" android:layout_height="196dp" android:layout_marginTop="64dp" android:layout_marginLeft="0dp" android:layout_marginRight="0dp" android:background="@color/white" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@id/menuLayout" /> ... </androidx.constraintlayout.widget.ConstraintLayout> 我正在尝试使用名为 GraphPlotter 的自定义类在其上绘制图形 GraphPlotter.java public class GraphPlotter extends View { private Paint paint = null; private Bitmap bitmap; private Canvas canvas; public GraphPlotter(Context context) { super(context); this.init(); } /* Initiates graph plotter */ private void init() { this.paint = new Paint(); this.paint.setColor(Color.BLUE); this.paint.setStrokeWidth(5); this.paint.setStyle(Paint.Style.FILL_AND_STROKE); // Create a bitmap and a canvas associated with the bitmap this.bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888); this.canvas = new Canvas(bitmap); } public void setColor(int color) { this.paint.setColor(color); invalidate(); // Force a redraw } /* Draws a line with a custom color */ public void drawLine(float starX, float startY, float stopX, float stopY,int color) { this.setColor(color); this.canvas.drawLine(starX, startY, stopX, stopY, this.paint); } ........ 这是我要画的活动 MainPageActivity.java public class MainPageActivity extends AppCompatActivity { private ConnectionHelper connectionHelper = null; private SQLQuery sqlQuery = null; private Person person = null; private TextView txtID = null; private RecyclerView recyclerView = null; private final ProductAdapter productAdapter = null; private AsyncTaskRunner asyncTaskRunner = null; private ViewHolder viewHolder = null; private Spinner graphicTypeSpinner = null; private Button menuButton = null; private ConstraintLayout menuView = null; private List<Product> productList = null; private GraphPlotter graphPlotter = null; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); // necessary for onCreate function setContentView(R.layout.main_page_layout); // R = resource , R.layout => layouts directory init(); LinearLayout graphicView = (LinearLayout) findViewById(R.id.graphicView); graphPlotter = new GraphPlotter(this); graphPlotter.drawAxisX(159,159,259, Color.BLUE); graphicView.addView(graphPlotter); } ........... ................... 但是我看不到任何线或轴。 我想使用画布为产品绘制图形,但我看不到任何结果,我的类或代码有什么问题 将您的绘制行为放入 onDraw() 函数中 @Override protected void onDraw(Canvas canvas) { drawLine(...); drawAxisX(); }

回答 1 投票 0

点击外部弹出窗口设置为 false [setOutsideTouchable(false)] 不起作用

//创建布局充气器 尝试 { LayoutInflater 充气器 = LayoutInflater.from(this); //创建视图 最终视图menuview = inflater.inflate(R.layout.menu, (

回答 4 投票 0

所有膨胀的 TableRow 都具有相同的 TextView 值?

在运行时,用户填充 SQLite 数据库。 当用户在其 onCreate 方法中打开某个活动时,我会填充 TableLayout。 我使用 While 循环来执行此操作。我膨胀的所有行...

回答 1 投票 0

如何获取我在 XML 中设计的自定义视图并将其用作我的 MainActivity 中的新视图

我设计了我想要的自定义视图: 我设计了我想要的自定义视图的样子: <RelativeLayout> <ConstraintLayout> <ConstraintLayout> <TextView/> <ImageButton/> </ConstraintLayout> <ImageView/> </ConstraintLayout> </RelativeLayout> 如何将其实现到我的 CustomView.java 文件中,以便我可以在 MainActivity.java 和 Activity_layout.xml 中使用它? 我知道我应该使用 LayoutInflater,但所有关于如何以及为什么使用 Kotlin 或印地语的解释我也不明白 当然,我将指导您完成用 Java 实现自定义视图的过程。您是正确的,您可以使用 LayoutInflater 从布局 XML 文件中扩充您的自定义视图。让我们分解一下步骤: 创建您的自定义视图类: 首先创建一个扩展RelativeLayout 并用作自定义视图的Java 类。我们将此类命名为 MyCustomView。 import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; public class MyCustomView extends RelativeLayout { private ConstraintLayout innerConstraintLayout; private ConstraintLayout nestedConstraintLayout; private TextView textView; private ImageButton imageButton; private ImageView imageView; public MyCustomView(Context context) { super(context); init(context); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.custom_view_layout, this, true); // Initialize your views innerConstraintLayout = findViewById(R.id.innerConstraintLayout); nestedConstraintLayout = findViewById(R.id.nestedConstraintLayout); textView = findViewById(R.id.textView); imageButton = findViewById(R.id.imageButton); imageView = findViewById(R.id.imageView); } // Add methods to interact with your custom views } 创建您的自定义视图布局: 创建一个表示自定义视图的视觉布局的 XML 布局文件。我们将此布局文件命名为custom_view_layout.xml。将此文件放入您的 res/layout 目录中。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/innerConstraintLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/nestedConstraintLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" /> <ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_foreground" /> </androidx.constraintlayout.widget.ConstraintLayout> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_foreground" /> </androidx.constraintlayout.widget.ConstraintLayout> </RelativeLayout> 在 MainActivity 中使用自定义视图: 现在,您可以在 MainActivity 中使用自定义视图。将自定义视图的实例添加到活动的布局 XML 文件 (activity_layout.xml) 中,或直接以编程方式将其添加到 MainActivity 类中。 MyCustomView customView = new MyCustomView(this); // Set layout params if needed setContentView(customView);

回答 1 投票 0

膨胀视图以供 AlertDialog 使用时发出警告“避免将 null 作为视图根传递”

我收到 lint 警告,在以 null 为父视图扩充视图时,避免将 null 作为视图根传递,例如: LayoutInflater.from(context).inflate(R.layout.dialog_edit, null); 然而,观点是......

回答 11 投票 0

动态添加的视图不可见

我试图在运行时将 TextViews 添加到包裹在 ScrollView 中的 LinearLayout,但 TextViews 的可见性最终不会显示在屏幕上,即使它们的可见性标记为...

回答 1 投票 0


在Adapter类中膨胀视图时出现InvocationTargetException异常

我试图膨胀一个自定义的回收视图,但问题是每次我试图在onCreateViewHolder()中膨胀一个xml文件时,它都会产生一个InvocationTargetException的异常。I ...

回答 1 投票 0

错误。"android.view.InflateException.Binary XML file line #7: Binary XML file line #7: Error inflating class <unknown> Binary XML file line #7: Binary XML file line #7: Error inflating class <unknown> "

当使用4.7 WXGA API 23时,我得到了 "android.view.InflateException.Binary XML file line #7: Binary XML file line #7: Error inflating class "的错误,但在更高的版本中,它是......。Binary XML file line #7: Binary XML file line #7: Error inflating class" 但在更高版本中,它是......

回答 1 投票 0

无法从膨胀的布局中找到ImageView

我已经创建了一个布局,并且正在尝试绑定其ID。我已经设法绑定到TextView,但是由于某种原因我找不到imageView id。 public FeedsAdapter(@NonNull上下文...

回答 2 投票 0

如何将搜索图标移到操作栏的右侧?

搜索栏应该在这里:在运行时,它在这里:这是我的菜单活动的代码:xmlns:app =“ http://schemas.android.com/apk/res-auto” >

回答 1 投票 0

android中的LayoutInflater无法以编程方式添加子级吗?

我有一个LinearLayout,我想使用LayoutInflater llMain =(LinearLayout)findViewById(R.id.llMain);以子级的方式在其中添加以编程方式添加的CardView作为孩子。 LayoutInflater ...

回答 1 投票 -1

LayoutInflater无法以编程方式添加子级吗?

我有一个LinearLayout,我想使用LayoutInflater llMain =(LinearLayout)findViewById(R.id.llMain);以子级的方式在其中添加以编程方式添加的CardView作为孩子。 LayoutInflater ...

回答 1 投票 0

在用户输入的列表视图中动态显示项目

我有两个编辑文本,用户在其中输入他们的名字和姓氏。我想在列表视图中显示以下名称和姓氏。因此,如果用户是第一次输入名称和姓氏,则...

回答 1 投票 0


Fragment getLayoutInflater()与LayoutInflater.from(getContext())

谁能在以下代码段中解释A和B有什么区别?它来自DialogFragment。重写fun onCreateDialog(savedInstanceState:Bundle?):对话框{// A)此...

回答 2 投票 0

如何仅一次膨胀按钮?

我只想使用充气机一次创建动态按钮。我有三个按钮,我有一个加载功能。该函数在每个按钮中以及每次调用时都被调用,预构建的按钮为...

回答 1 投票 0

具有新布局的空指针异常

我遇到了下一个问题:我有一个活动,它的布局名为camera_layout,当我在该文件中的所有布局都正常时,现在可以显示扩展后的某些视图...

回答 2 投票 0

我在底部导航中使用了tabview,但是未显示recyclerview

我放大了视图并为Tab创建了xml布局。我正在获取视图Textview Edittext,但是Recyclerview不在选项卡视图中显示

回答 3 投票 0

使用Layout Inflater方法为动态创建的EditText创建TextView

我是自学成才,坠入爱河。从理论上讲,这是我的第一个android应用程序,非常简单。问题:单击按钮,我成功地动态创建了新的EditText ...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.