我有一个相对布局,我正在以编程方式创建:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)
现在我有两个按钮,我想在这个相对布局中添加.但问题是RelatiiveLayout左侧的两个按钮都显示在彼此之间.
buttonContainer.addView(btn1);
buttonContainer.addView(btn2);
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何以编程方式设置按钮的android:layout_alignParentRight="true"或 android:layout_toLeftOf="@id/btn"属性",就像我们在xml中一样?
在所有Android标记上获取未知属性错误.
在布局XML中,自动建议不显示所有属性(如layout_width,layout_height,orientation,orientation和所有其他android属性.)
这是一个快照
SDk是最新的.
在App Level Gridle中
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.abhishek.ondemandservice"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Run Code Online (Sandbox Code Playgroud)
应用水平Gride.
buildscript {
repositories {
maven { url "http://dl.bintray.com/populov/maven" }
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle …Run Code Online (Sandbox Code Playgroud) android android-layout android-xml android-studio android-layoutparams
我想根据某些条件以编程方式删除和添加约束.以下是截图:
我想删除它,但在代码中:
所以想要以编程方式实现同样的效果
这是我试过的代码:
if (advertisements.size() > 0) { //my own condition
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
layoutParams.topToBottom = R.id.imvEmpty; //the imageview that is in center of the view
btnCreateAd.setLayoutParams(layoutParams);
recyclerView.setVisibility(View.VISIBLE);
txvMyAdEmptyText.setVisibility(View.GONE);
imvEmpty.setVisibility(View.GONE);
adapter.setList(advertisements);
adapter.notifyDataSetChanged();
} else {
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
layoutParams.topToBottom = -1; //here i am trying to remove top constraint but it doesn't work
btnCreateAd.setLayoutParams(layoutParams);
recyclerView.setVisibility(View.GONE);
txvMyAdEmptyText.setVisibility(View.VISIBLE);
imvEmpty.setVisibility(View.VISIBLE);
adapter.setList(new ArrayList<Advertisement>());
}
mConstraintView.invalidate(); //this is my constraint view
Run Code Online (Sandbox Code Playgroud)
编辑
我也尝试过使用 ConstraintSet,但结果甚至不同,我的RecyclerView(设置为父视图的边界)正在消失
ConstraintSet set = new …Run Code Online (Sandbox Code Playgroud) android android-layout android-layoutparams android-constraintlayout
我试图创建一个高于其他应用程序的视图:
WindowManager.LayoutParams paramsDirectorView = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
Run Code Online (Sandbox Code Playgroud)
我查看了其他回复,发现了以下"绘制应用程序"的内容:
我仍然得到" - 窗口类型2038的权限被拒绝"错误.截至目前我正在使用TYPE_PHONE并且它可以工作,但它已被弃用并且说要使用TYPE_APPLICATION_OVERLAY.有人可以对此进行跟进,因为TYPE_PHONE答案不是真正的解决方案,而是Android O中不推荐使用的"补丁工作"解决方案.
我在Android 7.1.2上运行
android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl$W@1f47e89 - android.app.ActivityThread上android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3344)的窗口类型2038的权限被拒绝.- wrap21(ActivityThread.java)位于android.app.AooT.Thread上的android.O. HandleMessage(Handler.java:102)上的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1583).(Looper) .java:154)在android.app.ActivityThread.main(ActivityThread.java:6121)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit) .java:889)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)引起:android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl$W@1f47e89 - 在Android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)的android.view.ViewRootImpl.setView(ViewRootImpl.java:703)上的窗口类型2038的权限被拒绝 HeadService.TwoViewManager的android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93).(两个ViewManager.java:99)位于android.app.ActivityThread.handleServiceArgs(ActivityThread)的HeadService.UIHeadService.onStartCommand(UIHeadService.java:65). java:3327)在Android.app.Handler.dispatchMessage(Handler.java:102)的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1583)的android.app.ActivityThread.-wrap21(ActivityThread.java)在android.os.Looper.loop(Looper.java:154)的android.app.ActivityThread.main(ActivityThread.java:6121),位于com.android.internal的java.lang.reflect.Method.invoke(Native Method) .os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:889)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
如何以android:layout_alignParentRight="false"编程方式设置?我可以true通过此代码将其设置为但我想以false编程方式将其设置为:
RelativeLayout.Layoutparams params =
(RelativeLayout.LayoutParams)myView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
有一个方法removeRule(int)但它被添加(API级别17).我想在API级别7或8中使用类似的东西.
我修改了一个线性布局,通过调整左边的孩子的大小来响应触摸,而右边的孩子占据了剩余的空间,模拟了一个可以使用以下代码"打开"或"关闭"的水平滚动
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) leftView.getLayoutParams();
//Log.d("ll","width " + newWidth);
lp.width=newWidth;
leftView.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)
在本机反应中,touch仍然调用此方法并记录预期值,但不更新子项的大小.它更新的唯一时间是我将可见性切换为已消失然后再次可见.从java调用invalidate/requestLayout或从js调用forceUpdate没有任何效果.
我需要调用一些其他代码来使视图无效并重绘吗?我需要提供一些提示,以便该组件滚动或响应触摸吗?
android scroll android-layout react-native android-layoutparams
我有一个CardViewXML布局,我需要以编程方式设置边距(这是因为我不需要任何时间的边距.基于几个条件,我设置或删除边距).
我是这样做的:
CardView.LayoutParams layoutParams = (CardView.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)
这很好.它把2dp保证金放在我的底部CardView.但是,我在logcat中得到了这个:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
Run Code Online (Sandbox Code Playgroud)
所以我改成CardView.LayoutParams了FrameLayout.LayoutParams,像这样:
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)
然而,它仍然有效,但我得到与上面相同的错误.
所以我再修改一次使用它LinearLayout.LayoutParams.
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) myCardView.getLayoutParams();
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0);
myCardView.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我没有得到错误,但它没有设置任何余量.
我应该忽略错误吗?这似乎不对.
编辑:
这是我CardView的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:background="@android:color/white"
android:minHeight="@dimen/item_min_height"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cardViewAppointmentWeek"
app:cardElevation="2dp"
android:layout_marginBottom="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"> …Run Code Online (Sandbox Code Playgroud) android android-layout layoutparams android-cardview android-layoutparams
我已经阅读了标准的Windows相关文档,并通过一堆源代码,试图了解如何以及何时创建Android Windows.我相信我搂着它,并希望得到验证或纠正.
据我所知,只有两种方法来获取Window对象的句柄.
1. Activity's getWindow.
2. Dialog's getWindow method.
Run Code Online (Sandbox Code Playgroud)
在上述两种情况下,您都可以使用getWindow获取Window的句柄,然后使用句柄操作Window.
Windows也可以通过使用WindowManager的addView方法创建,但是无法获得此类Windows的句柄.这是一个非常令人困惑的区域,因为addView方法本身并不意味着创建一个Window,甚至在View Manager源代码中对它的注释只是说明如下.
Assign the passed LayoutParams to the passed View and add the view to
the window.
Run Code Online (Sandbox Code Playgroud)
因此,该方法似乎意味着将View添加到现有Window.但是,addView的第二个参数是一个WindowManager.LayoutParams实例,除其他外,它指定一个Window类型(例如,TYPE_SYSTEM_ALERT),暗示实际上正在创建一个Window.事实上,情况就是如此.以下是源代码中发生的事情的简要概述.(对于新手:您可以在许多不同的网站上浏览源代码;我最喜欢的是http://grepcode.com.)
WindowManager实现了ViewManager接口,这是定义addView的地方.网址:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/ViewManager.java#ViewManager.addView%28android.view.查看%2Candroid.view.ViewGroup.LayoutParams 29%
WindowManager类的实际实现称为WindowManagerImpl.它的addView调用WindowManagerGlobal的addView.网址:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/WindowManagerImpl.java#83
WindowManagerGlobal的addView是真正完成工作的地方.网址:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/WindowManagerGlobal.java#WindowManagerGlobal.addView%28android.view.查看%2Candroid.view.ViewGroup.LayoutParams%2Candroid.view.Display%2Candroid.view.Window 29%
ViewRootImpl的setView方法然后做了一堆低级工作.它会在各种错误情况下抛出"无法添加窗口..."消息的变体异常.根据ViewRootImpl的主要评论,它是"视图层次结构的顶层,在View和WindowManager之间实现所需的协议.这在很大程度上是WindowManagerGlobal的内部实现细节."
网址:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/ViewRootImpl.java#ViewRootImpl.setView%28android.view.查看%2Candroid.view.WindowManager.LayoutParams%2Candroid.view.View 29%
WindowManagerGlobal似乎通过三个阵列跟踪多个Windows.据我所知,ViewRootImpl实际上是新窗口,它由WindowManagerGlobal管理.
基于以上所述,看来调用addView实际上创建了一个新窗口(尽管它被不同地称为视图和窗口)但是没有提供相关的Window类来访问它,因此开发人员不能得到它的句柄.
我的问题是:
谢谢!巴里
我正在关注好的回购,它展示了如何制作折叠工具栏WhatsApp-ProfileCollapsingToolbar的自定义行为.
我不喜欢的是当工具栏下方的图片(工具栏的字体为白色)为白色时,工具栏不可见.所以我试图将工具栏的背景设置为某种颜色.
首先我添加到widget_header_view.xml android:background="@android:color/holo_red_light",现在我喜欢它:
<?xml version="1.0" encoding="utf-8"?>
<com.anton46.whatsapp_profile.HeaderView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/white"
android:textSize="@dimen/header_view_start_text_size"
android:textStyle="bold" />
<TextView
android:id="@+id/last_seen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="@android:color/white" />
</com.anton46.whatsapp_profile.HeaderView>
Run Code Online (Sandbox Code Playgroud)
而在activity_main.xml中我已经改变了app:contentScrim="?attr/colorPrimary"对app:contentScrim="@android:color/holo_red_light"
但是因为这个repo在WhatsappHeaderBehavior中使用边距效果如下:
但我希望它像:
编辑1:
/sf/users/240532561/在/sf/answers/26071/80227/2401535中提出的带有填充的解决方案没有帮助,因为"浮动"工具栏覆盖了后退按钮,如下所示:

android android-layout android-toolbar android-collapsingtoolbarlayout android-layoutparams
是否可以指定每个窗口的 z 顺序?在下图中,每个 editText 都位于通过 WindowManager 添加的自己的窗口中。正如你所看到的,我有一个 z 顺序问题
正如您在图片中看到的那样,最后一个窗口添加了接缝以采用更高的 z 顺序。因此,我发现将窗口放在较高 z 顺序的唯一方法是从 windowManager 中删除视图,然后立即再次添加它。这不是最佳解决方案,因为视觉上视图消失并重新出现在屏幕上
android android-layout android-view android-windowmanager android-layoutparams
android ×10
android-collapsingtoolbarlayout ×1
android-view ×1
android-xml ×1
layoutparams ×1
react-native ×1
scroll ×1