Tag: android layout

状态栏不透明,但是白色

用anko DSL测试kotlin,我决定使用kotlin插件(1.0.3)和最新的anko库(0.9)在最后的android studio ide(2.1.3)中启动一个新的proyect, 我使用默认的导航抽屉活动,所以我只需要将主要的XML转换为ANK。 这是xml: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <TextView android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> […]

如何以编程方式在RelativeLayout中布局视图?

我试图通过编程实现以下(而不是通过XML声明): <RelativeLayout…> <TextView … android:id="@+id/label1" /> <TextView … android:id="@+id/label2" android:layout_below: "@id/label1" /> </RelativeLayout> 换句话说,如何让第二个TextView出现在第一个TextView下面,但我想在代码中做到这一点: RelativeLayout layout = new RelativeLayout(this); TextView label1 = new TextView(this); TextView label2 = new TextView(this); … layout.addView(label1); layout.addView(label2); setContentView(layout); 更新: 谢谢,TreeUK。 我理解大方向,但仍然不起作用 – “B”重叠“A”。 我究竟做错了什么? RelativeLayout layout = new RelativeLayout(this); TextView tv1 = new TextView(this); tv1.setText("A"); TextView tv2 = new TextView(this); […]

使用DataBinding库设置背景颜色资源或null

我想使用DataBinding库在我的视图设置背景颜色或null ,但我得到一个异常,试图运行它。 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference 这是我如何做到的: android:background="@{article.sponsored ? @color/sponsored_article_background : null}" 我也尝试设置转换,但没有奏效。 @BindingConversion public static ColorDrawable convertColorToDrawable(int color) { return new ColorDrawable(color); } 最终,我用@BindingAdapter解决方法解决了它,但我想知道如何正确地做到这一点。

Android Studio – 获取活动视图实例的最佳方式

我是Android Studio的新手,我想快速访问按钮,图像视图,文本视图等视图。 到目前为止,我知道方法findViewById ,这就是我正在做的事情来创建轻松访问视图: Button btn1, btn2, btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.btn1=(Button)findViewById(R.id.btn1); this.btn2=(Button)findViewById(R.id.btn2); this.btn3=(Button)findViewById(R.id.btn3); } //then I simply use my defined vars 尽管它有效,但是编写所有这些代码仍然很无聊(或者每次都必须使用findViewById ,而获得id并且仍然添加一个cast的方法很麻烦)。 这真的是这样做的最好方法吗?

将线性转换为网格布局

给定的代码创建一个线性布局显示所有文件夹的布局。 它显示水平视图中的所有文件夹。 我想将其转换为网格布局,以便我可以在一个屏幕中容纳多个文件夹。 val root = LinearLayout(this) val file = File("/sdcard/MedProRecordings/").absoluteFile if (file.isDirectory == false) { Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show() } val files = file.listFiles() var i = 1 for (f in files!!) { if (f.isDirectory) { if(f.list().isNotEmpty()){ try { val layout = LinearLayout(this) layout.id = i val btnGreen = ImageButton(this) btnGreen.setImageResource(R.drawable.play) btnGreen.setBackgroundColor(Color.TRANSPARENT) layout.addView(btnGreen) root.addView(layout) btnGreen.setOnClickListener […]

在android中使用数据绑定在android:src中为ImageView设置drawable资源ID

我试图设置可绘制的资源ID到android:使用数据绑定ImageView的src 这是我的目标: public class Recipe implements Parcelable { public final int imageResource; // resource ID (eg R.drawable.some_image) public final String title; // … public Recipe(int imageResource, String title /* … */) { this.imageResource = imageResource; this.title = title; } // … } 这是我的布局: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="recipe" type="com.example.android.fivewaystocookeggs.Recipe" /> </data> […]

无法单击CollapsingToolbarLayout中的项目

我已经实现了如下所示的CollapsingToolbarLayout : 当我滚动的时候会崩溃,但是有一个很大的问题:我无法点击它里面的项目(后退按钮,编辑按钮,编辑文本框等)。 没有任何项目对点击/触摸事件作出反应。 现在,“逻辑”位于XML中,结构如下: <CoordinatorLayout> <AppBarLayout> <CollapsingToolbarLayout> <RelativeLayout/> here's where the panel layout sits <Toolbar/> <NestedScrollView/> //close tags as appropriate here 如何使项目可点击(当然,不是通过设置android:clickable="true"肯定)? 在Java/Kotlin代码中编写监听器是否可能?

TextView在设计预览中显示,但不在实际的Android应用程序中显示

我在第二个Activity中有一个XML布局,根据用户是否登录来显示两个LinearLayouts的一个,而TextView我从来没有在应用中显示文本,但是当我看到布局编辑器中的“设计”面板 TextView在那里(如果我设置背景颜色,它显示颜色),但文本本身从不呈现。 单击MainActivity上的一个按钮将导致启动具有onCreate (它是Kotlin)的LoginActivity : override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.login_layout) 和布局: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" android:id="@+id/loggedOut" android:padding="24dp"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/passName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:focusable="true" android:hint="Username" android:inputType="textCapCharacters" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/passPin" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:focusable="true" android:hint="PIN" android:inputType="numberPassword" /> </android.support.design.widget.TextInputLayout> <Button […]

滚动时,片段内容隐藏在BottomNavigationView后面

我已经深入研究了这一点,并且在做什么的时候感到不知所措 – 我在活动中的BottomNavigation栏截取了一个片段中的一些内容,我不知道该怎么做。 我尝试添加app:layout_behavior="@string/appbar_scrolling_view_behavior"到NestedScrollView但内容的底部(位置的名称)仍然被切断 – 这可能是一个简单的解决办法,但我不能想办法。 我的主要活动和“家”片段的XML如下所示: activity_home.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:id="@+id/home_screen" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/app_bar" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="start" android:background="@color/navbarBackground" app:menu="@menu/bottom_nav_menu" /> </LinearLayout> home_fragment.xml <?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:id="@+id/home_fragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="0dp"> <ImageView android:id="@+id/tokyo_placeholder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitStart" app:layout_constraintLeft_toLeftOf="parent" […]

在Android中从位图裁剪圆形区域

我有一个位图,我想从这个位图裁剪一个圆形的区域。 圆圈外的所有像素都应该是透明的。 我该怎么做?