触摸布局时,如何在简单的线性/相对布局中产生涟漪效应?
我已经尝试将布局的背景设置为:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" >
<item android:drawable="@android:color/white"/>
</ripple>
Run Code Online (Sandbox Code Playgroud)
然而,我只看到纯白色背景,并且在触摸布局时没有涟漪效应.我究竟做错了什么?
编辑:
作为参考,这是我的布局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="@drawable/test"
android:clickable="true">
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在以下条件下使用Lollipop上的列表视图控件:
注意:我被迫使用自定义列表选择器颜色,因为如果我使用白色背景,暗材料主题选择器使用主题的colorControlHighlight颜色为纹波,即40ffffff,并且不显示.
我首先尝试了以下内容:
布局xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
list_selector xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#ff0000" >
</ripple>
Run Code Online (Sandbox Code Playgroud)
并列出视图行xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables" >
<ImageView
android:id="@+id/list_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我期待看到的是当我选择一个项目时,选择的项目的颜色为#ff0000.然而,这是我最终看到的:
我希望的是有点接近这种行为 - 但限制在选定的列表行中!我究竟做错了什么?
谢谢,扎克
如果添加以下行,则在创建新的Android Studio项目(“空活动”类型)后:
// Lifecycle/View Models
def lifecycleVersion = '2.0.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion"
// Core testing
def coreTestingVersion = '2.0.1'
testImplementation "androidx.arch.core:core-testing:$coreTestingVersion"
androidTestImplementation "androidx.arch.core:core-testing:$coreTestingVersion"
Run Code Online (Sandbox Code Playgroud)
对于应用程序的build.gradle
文件,当您尝试运行connectedCheck
项目任务时会看到错误。查看输出:
Zachs-MBP:CoreTestingVersionExample Zach$ ./gradlew connectedCheck
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:preDebugAndroidTestBuild'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
> Could not resolve androidx.arch.core:core-common:2.0.0.
Required by:
project :app
> Cannot find a version of 'androidx.arch.core:core-common' that satisfies the …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用带有appbar布局的协调器布局,该布局将片段作为"滚动视图".该片段由一个recyclerView和一个底部对齐的布局组成,按住一个按钮,如下所示:
但是,默认情况下隐藏底部:
并且仅在我滚动后显示.
从我的活动课:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestFragment fragment = (TestFragment) getFragmentManager().findFragmentByTag("Test");
if (fragment == null)
fragment = new TestFragment();
getFragmentManager().popBackStack();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment, "Test");
fragmentTransaction.commit();
}
Run Code Online (Sandbox Code Playgroud)
活动布局:
<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="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="enterAlways|scroll"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
片段布局:
<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="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottomView"
android:scrollbars="vertical"
android:visibility="visible"/>
<RelativeLayout …
Run Code Online (Sandbox Code Playgroud) android appbar android-fragments coordinator-layout android-coordinatorlayout
Programmatically setting my FloatingActionButton
's backgroundTint
via setBackgroundTintList
method does not work, but setting it via XML app:backgroundTint
tag does work - why is that?
fab_background_color.xml 颜色列表状态为:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#654321"/>
<item android:color="#123456"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
我的活动布局是:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.FloatingActionButton
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
和活动代码:
public class SampleActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_position_sample);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.test);
// Uncomment to test - this does NOT work however.
//fab.setBackgroundTintList(getResources().getColorStateList(R.color.fab_background_color));
fab.setOnClickListener(new …
Run Code Online (Sandbox Code Playgroud) 我想在模型类中记录信息 - 不一定用于单元测试目的,而是用于我尝试调试的现实生活场景。
但是,如果我尝试使用android.util.Log
方法,在运行 JUnit 测试时会出现以下错误:
java.lang.RuntimeException: Method d in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.
Run Code Online (Sandbox Code Playgroud)
我明白为什么会发生这种情况,我不应该在设计为独立于框架的模型类中使用 Android 框架代码!我并不是真的反对这个错误,而是我试图找到一种方法来解决这个问题。
我有一个想法,这有意义吗?
CustomLog
沿着这些线创建一个类:
public class CustomLog {
private static ILogger mLogger;
public static void setLogger(ILogger logger) {
mLogger = logger;
}
public static void e(String tag, String message) {
mLogger.e(tag, message);
}
}
Run Code Online (Sandbox Code Playgroud)
ILogger
具有执行日志功能所需方法的接口在哪里(e、d 等方法...)
我可以创建一个ILoggerImpl
使用这些android.util.Log
方法的MockLogger
类,以及一个简单地打印出来System.out.println
和/或什么都不做(或其他任何事情!)的类。
我认为这完全符合我的需求(我需要CustomLog
在生命周期的早期设置我的类,但这不是什么大问题)。
但是,如果我需要将第三方库/外部代码添加到我的模型类中,如果新库/代码使用android.util.Log
方法,这可能会以相同的方式再次中断。
那么,是否有我可以使用的“一网打尽”类型的行为?你怎么认为?
当使用 AGP 7.2 (android studio Chipmunk) 运行 android 仪器测试时,我看到以下错误:
\n\njava.lang.NoClassDefFoundError: Failed resolution of: Lorg/jacoco/agent/rt/internal_b6258fc/Offline;\n at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.$jacocoInit(Unknown Source:13)\n at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.<clinit>(Unknown Source:0)\n at java.lang.Class.newInstance(Native Method)\n at android.app.Instrumentation.newActivity(Instrumentation.java:1174)\n at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)\n at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)\n at android.app.ActivityThread.-wrap11(Unknown Source:0)\n at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)\n at android.os.Handler.dispatchMessage(Handler.java:106)\n at android.os.Looper.loop(Looper.java:164)\n at android.app.ActivityThread.main(ActivityThread.java:6494)\n at java.lang.reflect.Method.invoke(Native Method)\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)\nCaused by: java.lang.ClassNotFoundException: Didn't find class "org.jacoco.agent.rt.internal_b6258fc.Offline" on path: DexPathList[[zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.abc.appname.develop.test-VAV8qDGNBpOvARX_Y9h_SA==/base.apk"],nativeLibraryDirectories=[/data/app/com.abc.appname.develop.test-VAV8qDGNBpOvARX_Y9h_SA==/lib/x86, /system/lib, /vendor/lib]]\n at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:379)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:312)\n at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.$jacocoInit(Unknown Source:13)\xc2\xa0\n at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.<clinit>(Unknown Source:0)\xc2\xa0\n at …
Run Code Online (Sandbox Code Playgroud) 我想知道如何在回收器视图中找到特定项目,其中每次运行时项目的顺序是随机的.
假设我在Recycler视图中有4个项目,每个项目都由相同类型的视图持有者表示,其中包含文本视图.每个视图持有者/项目都应用唯一标题.对于这个例子,假设为了简单起见,标题是"A","B","C"和"D".
如果订单是随机的,我如何找到位置(然后点击)项目"A"?我知道如果订单没有改变我可以使用scrollToPosition RecyclerViewInteraction操作,但在这种情况下,订单可以并且将会改变.
有什么想法吗?
谢谢,扎克
我在 Windows 7 机器上安装了 Jenkins 2.73.1,没有从站。构建 Android 应用程序设置只有一项工作。我将connectedCheck
gradle 任务作为作业构建过程的一部分执行。
当我重新启动计算机并运行 Jenkins 作业时,我看到以下输出:
23:15:46 :app:connectedDebugAndroidTest
23:15:46 Starting 166 tests on Nexus_5X_API_26(AVD) - 8.0.0
23:15:48
23:15:48 (packages).(testclass) > testMethod[Nexus_5X_API_26(AVD) - 8.0.0] [31mFAILED [0m
23:15:48 java.lang.AssertionError
23:15:48 at org.junit.Assert.fail(Assert.java:86)
23:15:49 There were failing tests. See the report at: file:(path to app) /app/build/reports/androidTests/connected/index.html
Run Code Online (Sandbox Code Playgroud)
这完全是预料之中的——我有一个失败的测试来“测试”詹金斯!
然而,在“使用”同一台机器进行开发工作后,我经常遇到以下问题(它与 Jenkins 共享 - 我知道,并排开发和 Jenkins 是 100% 错误的,但这对我来说是真正了解 Jenkins) ,如果可能的话,不要让它 100% 安全,我完全理解答案可能是隔离 dev/jenkins,但现在也许还有其他解决方案?)。我运行模拟器、单元测试等,最终 Jenkins 抱怨道:
00:27:19 :app:connectedDebugAndroidTest FAILED
00:27:19
00:27:19 FAILURE: Build failed with an exception.
00:27:19 …
Run Code Online (Sandbox Code Playgroud) 我最近升级到了Android Studio 3.2.1 / com.android.tools.build:gradle:3.2.1
,并注意到当我运行Instrumentation Tests时,有很多新输出阻塞了我要查看的日志。这是一个小预览:
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS_CODE: 1
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS: class=com.zach.testapp.ExampleInstrumentedTest
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS: current=1
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS: id=AndroidJUnitRunner
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS: numtests=1
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS: stream=.
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS: test=useAppContext
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_STATUS_CODE: 0
07:38:28 V/InstrumentationResultParser: INSTRUMENTATION_RESULT: stream=
对于具有单个Instrumentation测试的新项目,将显示超过50行文本作为输出。在我的“主要”项目中,我进行了数百次Instrumentation测试,这使输出很难读取有意义的信息(例如失败),因为我必须滚动很多。
有没有办法禁用此日志记录?
android android-studio android-gradle-plugin android-instrumentation