我有一个简单的全屏活动.布局检查器显示Android提供的大型根布局层次结构,我不需要全屏显示.
Run Code Online (Sandbox Code Playgroud)
调用Tools > Android > Layout Inspector以查看生成的根模板.
要使其全屏,只需HelloWorld在布局中添加一个ID 到TextView并更新MainActivity.java:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.blcknx.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
标志正在隐藏导航,但它们不会删除未使用的布局.
使用简单的TextView.全屏显示.检查Tools > Android > Layout Inspector您删除根布局的距离.显示截图.显示你的代码.
编写时Views,ViewModels并且LiveData具有生命周期意识。在ViewModel想是目前的FragmentActivity,LiveData当前的LifecycleOwner。您事先不知道您的 View 是否会被包裹或有所包裹。所以它需要一个灵活的函数来找到想要的上下文。我最终采用了这两种方法:
private FragmentActivity getFragmentActivity() {
Context context = getContext();
while (!(context instanceof FragmentActivity)) {
context = ((ContextWrapper) context).getBaseContext();
}
return (FragmentActivity) context;
}
private LifecycleOwner getLifecycleOwner() {
Context context = getContext();
while (!(context instanceof LifecycleOwner)) {
context = ((ContextWrapper) context).getBaseContext();
}
return (LifecycleOwner) context;
}
Run Code Online (Sandbox Code Playgroud)
现在这是要放入每个视图的大量样板代码。有没有更简单的方法?
我不想为此使用自定义的 View 基类,因为大的层次结构很难看。另一方面,组合需要与此解决方案一样多的代码。
我试图弄清楚如何以最佳方式完成ViewModel中的Activity.我找到了使用LiveData对象并发出"信号"的一种方法.
我怀疑这个解决方案有一个开销.那么它是正确的解决方案还是我应该使用更准确?
所以举个例子:让我们假设在一个应用程序中是一个活动MainActivity和视图模型,如下所示:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val model = ViewModelProviders.of(this).get(MainViewModel::class.java)
model.shouldCloseLiveData.observe(this, Observer { finish() })
}
}
Run Code Online (Sandbox Code Playgroud)
并且作为MainActivity的伴侣是MainViewModel,如下所示:
class MainViewModel(app: Application) : AndroidViewModel(app) {
val shouldCloseLiveData = MutableLiveData<Void>()
fun someAction(){
shouldCloseLiveData.postValue(null)
}
}
Run Code Online (Sandbox Code Playgroud) 我试着理解Button的默认背景(android 26).
sdk/platforms/android-26/data/res/drawable/btn_default_material.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="@drawable/btn_default_mtrl_shape" />
</ripple>
Run Code Online (Sandbox Code Playgroud)
引用:
sdk/platforms/android-26/data/res/drawable/btn_default_mtrl_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/button_inset_horizontal_material"
android:insetTop="@dimen/button_inset_vertical_material"
android:insetRight="@dimen/button_inset_horizontal_material"
android:insetBottom="@dimen/button_inset_vertical_material">
<ripple android:color="?attr/colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="@color/btn_colored_background_material">
<corners android:radius="@dimen/control_corner_material" />
<solid android:color="@color/white" />
<padding android:left="@dimen/button_padding_horizontal_material"
android:top="@dimen/button_padding_vertical_material"
android:right="@dimen/button_padding_horizontal_material"
android:bottom="@dimen/button_padding_vertical_material" />
</shape>
</item>
</ripple>
</inset>
Run Code Online (Sandbox Code Playgroud)
我理解插图.这是按钮背景图像周围的空间.我不明白填充.背景图像中没有文字.
提出一个思考方法:如果删除padding标签会发生什么?
你可能会得出结论,没有任何事情会发生(我不确定,什么都不会发生).但如果没有任何反应,你有一个问题是什么对它有用?他们为什么把它放在那里?
组织专业Android应用程序按钮样式的最佳实践是什么?假设更大的当代应用程序(SDK 26 +,min SDK 21).
这个问题是可以回答的,因为它们的来源Material Design和设置都Android Studio提供了足够的线索和预期专业用途模式的例子.当然,用户不仅限于这种模式,而是遵循它们,使应用程序与源代码一起发挥良好Material Design并提供最佳可维护性.
我找到了几个与按钮造型相关的成分.
@style/Widget.AppCompat.Button@style/Widget.AppCompat.Button.Colored@style/TextAppearance.AppCompat.Button@style/TextAppearance.AppCompat.Widget.Button@color/foreground_material_dark?colorAccent?textColorPrimary?android:colorForeground?textAppearanceButton可能会有更多.
您可以查找来源.但是,即使知道所有细节也无法全面了解预期用途.这个问题要求绘制图片.
android android-layout android-fonts android-studio android-color
我正在开发 npm 命令行工具和包https://github.com/ecma-make/ecmake。我在该包的全局安装版本和本地安装版本之间遇到了奇怪的冲突。
我可以通过将一个库链接到另一个库来避免这种冲突。那么该库就只有一个实例,不存在冲突。现在我必须考虑用户,他确实将包安装到两个地方,一次是全局安装,以便能够在没有前缀的npx情况下运行命令,一次是本地安装,以便在package.json.
# prepare test fixture
mkdir ecmakeTest
cd ecmakeTest/
npm init -y
# install globally
npm install -g @ecmake/ecmake@0.3.1
npm ls -g @ecmake/ecmake
# install locally
npm install --save-dev @ecmake/ecmake@0.3.1
npm ls @ecmake/ecmake
# init ecmakeCode.js
npx ecmake --init
# run with local lib => shows the expected behaviour
npx ecmake all
# run with global lib => NoRootTaskError
ecmake all
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪引导我们到全局安装中的行:/usr/local/lib/node_modules/@ecmake/ecmake/lib/runner/reader.js:21:13。
if (!(root instanceof Task)) …Run Code Online (Sandbox Code Playgroud) 我的问题是相反的方向。如何从SimpleNamespace初始化字典?
什么是 TYPO3 参考索引?为什么它会包含过时的值?
在视网膜显示时,CSS 中的像素是什么?
关于 px 的问题已经回答过了。答案似乎有点过时,并没有解决今天的问题。或者他们已经在现在得到了回答,但在更复杂的背景下。
所以对于 2019 年,一个简单的问题是:CSS 中的 px 是什么?
android ×5
android-view ×2
android-architecture-components ×1
backend ×1
command-line ×1
css ×1
javascript ×1
kotlin ×1
makefile ×1
module ×1
mysql ×1
node.js ×1
python ×1
python-3.x ×1
typo3 ×1