我尝试在 XML 中使用我自己的 SurfaceView,但无法做到这一点。我得到 NullPointerException。根据互联网,它应该看起来像这样:
活动:
package editor;
import android.app.Activity;
import android.os.Bundle;
import com.example.balls_menu_v1.R;
public class EditorActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
EditorView ev = (EditorView) findViewById(R.id.editorView);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我发表评论,findViewById我会得到 NullPointerException。
表面视图:
package editor;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class EditorView extends SurfaceView {
public EditorView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onFinishInflate() {
super.onFinishInflate();
SurfaceHolder holder = getHolder();
Canvas …Run Code Online (Sandbox Code Playgroud) 使用以下 I 代码,我试图获取 xml 中的节点列表,但应用程序崩溃并引发 saxparser 异常。
这是我尝试使用的 xml
<?xml version="1.0"?><Root><ResponseCode>1</ResponseCode><ResponseMessage>Reported Successfully</ResponseMessage><ResultSet><Post><id>1</id><title>Garbage Problem near my home</title><comment>We the citizens have been facing the problems of garbage since 2004 , kindly fix it , authorities are concerned</comment><imagepath></imagepath><cordx>24.818688</cordx><cordy>67.029686</cordy><tag>Garbage</tag><userid>1</userid><departmentid>1</departmentid><response></response><createdOn>2013-03-23 14:44:43</createdOn><status>2</status></Post></ResultSet></Root>
Run Code Online (Sandbox Code Playgroud)
这是我试图用来从 xml 中获取的代码
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(result));
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
String code = xPath.evaluate("/Root/ResponseCode/text()", is);
if(code.compareTo("1")==0){
Log.d("Responce Code", code);
NodeList nodeList = (NodeList) xPath.evaluate("//Post", is,XPathConstants.NODESET);
for(int i=0; i<nodeList.getLength(); i++){
Node n=nodeList.item(i);
Element element = (Element) n; …Run Code Online (Sandbox Code Playgroud) 我在 android 的框架布局中添加了一个内部图像。但问题是我得到了白色背景。我需要删除那个白色背景。任何帮助将不胜感激......
截图如下

xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@null"
android:maxWidth="-5dp"
android:maxHeight="-5dp"
android:src="@drawable/driveimg"
/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud) 这是我得到的错误:
Execution failed for task ':MyPackage:processReleaseResources'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/abc/android-sdk-macosx/build-tools/22.0.1/aapt'' finished with non-zero exit value 1
No resource found that matches the given name (at 'text' with value '@string/secondary_text').
Run Code Online (Sandbox Code Playgroud)
我的@string/secondary_text是这样的:
<string name="secondary_text">They're all free to download.</string>
Run Code Online (Sandbox Code Playgroud)
我在这里转义了单引号,我'也尝试过,但仍然不起作用。唯一有效的是我They're改为They are.
有谁知道如何在 Android 上转义单引号?
当我多次将额外的属性应用到 AppTheme 时,它会覆盖前一个属性。这是我正在使用的代码:
主要活动:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
getTheme().applyStyle(R.style.AppTheme_OverlayPrimaryRed);
// If you change `false` to `true`, the overlay above is overwritten.
if (false) {
getTheme().applyStyle(R.style.AppTheme_OverlayAccentRed);
}
super.onCreate(savedInstanceState);
...
}
Run Code Online (Sandbox Code Playgroud)
AppTheme.OverlayPrimaryRed:
<style name="AppTheme.OverlayPrimaryRed">
<item name="colorPrimary">@color/material_red_500</item>
<item name="colorPrimaryDark">@color/material_red_700</item>
</style>
Run Code Online (Sandbox Code Playgroud)
AppTheme.OverlayAccentRed:
<style name="AppTheme.OverlayAccentRed">
<item name="colorAccent">@color/material_red_A200</item>
</style>
Run Code Online (Sandbox Code Playgroud)
有什么想法可以解决这个问题吗?
android android-xml android-theme android-activity android-styles
我在为导航抽屉中的菜单项使用自定义视图时遇到了一些问题。当我使用android:actionLayout任何东西时,尽管有一个有效的布局。显示项目文本,但未显示自定义布局中的任何内容。从我在文档中读到的内容来看,我不应该为此需要任何 java 代码,并且应该能够纯粹在 xml 中进行设置,对吗?
我的活动抽屉和actionLayout xml文件如下:
activity_main_drawer.xml 文件:
<group android:checkableBehavior="none" >
<item ... />
<item ... />
<item ... />
</group>
<group android:id="@+id/menu_bottom" android:checkableBehavior="none">
<item ... />
</group>
<group android:id="@+id/bookmark_list" android:checkableBehavior="single">
<item
android:id="@+id/nav_bookmark"
android:actionLayout="@layout/nav_bookmark_header_layout"
android:title="Some text"
app:showAsAction="ifRoom" />
</group>
Run Code Online (Sandbox Code Playgroud)
layout/nav_bookmark_header_layout.xml 文件:
<TextView
android:id="@+id/counterView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom text"/>
Run Code Online (Sandbox Code Playgroud)
我想在我的font_family.xml. 我fontStyle="bold"在字体系列开发人员文档中没有找到粗体。它只有normal或italic属性。这是font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/lobster_italic" />
</font-family>
Run Code Online (Sandbox Code Playgroud)
所以我必须使用像
<style name="fontBoldStyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/montserrat_semi_bold</item>
</style>
Run Code Online (Sandbox Code Playgroud)
或者像这样
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat_semi_bold"/>
Run Code Online (Sandbox Code Playgroud)
我的问题
fontStyle属性呢?即使我没有提供斜体字体font_family.xml,然后设置textStyle="italic"我的字体是斜体。android:fontStyle="bold",font_family.xml这样我就不必创建不同的样式,我只需要设置textStyle="bold"那么我可以将 3 种字体设置normal, bold, italic为 font_family.xml 并android:fontFamily="@font/font_family"在 TextView 上设置然后我可以使用android:textStyle="bold"斜体或普通字体的解决方案是什么?
如何在未TextInputLayout聚焦时更改边框和颜色?
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_height="wrap_content"
app:boxStrokeWidth="4dp"
app:boxStrokeColor="@color/colorPrimary"
app:layout_constraintTop_toBottomOf="@id/a">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:hint="@string/app_name"
android:textColorHint="@color/colorPrimary"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我尝试:
app:boxStrokeWidth="4dp"
app:boxStrokeColor="@color/colorPrimary"
Run Code Online (Sandbox Code Playgroud)
但是,不能在不聚焦的情况下工作
android android-xml android-textinputlayout material-components material-components-android
最近我从 ButterKnife 转向 Android View Binding。我的所有布局都运行良好,但是迁移到 View Binding 后,出现了一个奇怪的行为,所有 RecyclerView 项目都有 aslayout_width而wrap_content不是match_parent。
当我查看布局检查器工具时,我发现卡片视图wrap_content的宽度有价值。
但是,我没有更改 xml 文件,因此宽度实际上仍然是match_parent。
我错过了什么吗?
如何创建布局或视图,如附加图像、可用于 ImageView、Slider 等的形状。
android curve android-layout android-xml androiddesignsupport
android ×10
android-xml ×10
curve ×1
font-family ×1
image ×1
material-components-android ×1
surfaceview ×1
xml ×1
xml-parsing ×1
xpath ×1