Tag: android layout

NestedScrollView中的ViewPager

我需要创build一个像Google Newsstand这样的界面,它是一种在折叠标题(垂直滚动)上的ViewPager(水平滚动)。 我的要求之一是使用Google IO 2015中提供的新devise支持库( http://android-developers.blogspot.ca/2015/05/android-design-support-library.html ) 基于由Chris Banes( https://github.com/chrisbanes/cheesesquare )创build的示例,我已经达到了能够使用基本LinearLayout(无水平滚动)进行折叠行为的要点。 我试图用ViewPagerreplaceLinearLayout,然后我得到一个空白的屏幕。 我玩的宽度,重量和各种观点组,但….仍然是一个空白的屏幕。 看来,ViewPager和NestedScrollView不喜欢对方。 我通过使用Horizo​​ntalScrollView尝试了一种解决方法:它可以工作,但是我放松了PagerTitleStripfunction的好处,并且关注单个面板(我可以在2个面板之间水平停​​止)。 现在我没有更多的想法,如果任何人都可以让我find一个解决scheme… 谢谢 这是我最新的布局文件: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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.AppBarLayout android:layout_width="match_parent" android:layout_height="@dimen/header_height" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="48dp" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <include layout="@layout/part_header" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_collapseMode="parallax"/> <android.support.v7.widget.Toolbar android:id="@+id/activity_main_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/activity_main_nestedscrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" […]

View.setPadding只在px中接受,反正在dp中有setPadding吗?

Android View.setPadding(int left, int top, int right, int bottom)只接受px中的值,但是我想在dp中设置填充。 有没有办法解决它?

在Intellij IDEA / Android Studio中预览具有合并根标签的布局

假设我们正在开发基于LinearLayout的复合组件。 所以,我们创build这样的类: public class SomeView extends LinearLayout { public SomeView(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(LinearLayout.VERTICAL); View.inflate(context, R.layout.somelayout, this); } } 如果我们将使用LinearLayout作为somelayout.xml的根,我们将有额外的视图级别,所以我们使用合并标签: <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Some text" android:textSize="20sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Some other text"/> </merge> 但在IDE合并的预览选项卡中,总是充当FrameLayout,我们将看到类似这样的内容: (这是Android Studio,Intellij IDEA是一样的,关于Eclipse我不知道) 预览加速开发布局很多,即使对于一些布局,也不会失去这么大的帮助。 可能是有一种方法来指定,如何预览应解释merge标签在特定的布局?

在使用xml的android textview中使用自定义字体

我已经将自定义字体文件添加到我的资产/字体文件夹。 我如何从我的XML中使用它? 我可以从代码中使用它,如下所示: TextView text = (TextView) findViewById(R.id.textview03); Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf"); text.setTypeface(tf); 我不能从XML使用android:typeface="/fonts/Molot.otf"属性吗?

如何以编程方式围绕angular落并设置随机背景颜色

我想绕过视图的angular落,并根据运行时的内容更改视图的颜色。 TextView v = new TextView(context); v.setText(tagsList.get(i)); if(i%2 == 0){ v.setBackgroundColor(Color.RED); }else{ v.setBackgroundColor(Color.BLUE); } v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); v.setPadding(twoDP, twoDP, twoDP, twoDP); v.setBackgroundResource(R.drawable.tags_rounded_corners); 我希望设置drawable和颜色会重叠,但他们不。 无论我执行第二个是由此产生的背景。 有没有办法以编程方式创build此视图,请记住,直到运行时才会决定背景颜色? 编辑:我只是现在换红色和蓝色进行testing。 稍后颜色将由用户select。 编辑: tags_rounded_corners.xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:bottomRightRadius="2dp" android:bottomLeftRadius="2dp" android:topLeftRadius="2dp" android:topRightRadius="2dp"/> </shape>

在Android中从ScrollView删除滚动条轨道

我的Android应用程序有一个主要的WebView(从本地资源加载的HTML),我想要使用整个屏幕的宽度,并能够(垂直)滚动。 所以我已经在我的布局XML的ScrollView中包装了WebView,但无论我做什么,我都无法从滚动视图的右侧移除滚动条轨道。 更糟的是,我似乎无法改变滚动条轨道的背景颜色。 该轨道占用约10dp左右,这是在WebView的HTML创build问题。 我想滚动条出现在 Web视图的顶部 (iPhone风格,如果你知道我的意思)。 现在,你可以说“你为什么不改变你的HTML 10px更薄?”,这是我的后备解决scheme,但我宁愿不必。 这里是相关的布局XML片段,你会看到我已经尝试了每个可以find的android:etc属性: <ScrollView android:id="@+id/deal_web_view_holder" android:layout_below="@id/clock_bar_holder" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="false" android:fadingEdge="none" android:background="#02a7e9" android:scrollbars="none" android:scrollbarSize="0dp" android:paddingRight="0dp" android:scrollbarAlwaysDrawVerticalTrack="false" android:scrollbarStyle="insideOverlay" android:scrollbarTrackVertical="@drawable/scrollbar_track_vertical" > <WebView android:id="@+id/deal_web_view" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </ScrollView> 我的目标是平台2.1 / API lvl 7,真的只是处理正常大小的显示器,mdp,hdp和xhdp。

Authorative方式来覆盖onMeasure()?

什么是重写onMeasure()的正确方法? 我见过各种方法。 例如, 专业Android开发使用MeasureSpec来计算维度,然后结束调用setMeasuredDimension()。 例如: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int parentWidth = MeasureSpec.getSize(widthMeasureSpec); int parentHeight = MeasureSpec.getSize(heightMeasureSpec); this.setMeasuredDimension(parentWidth/2, parentHeight); } 另一方面,根据这篇文章 ,“正确的”方法是使用MeasureSpec,调用setMeasureDDimensions(), 然后调用setLayoutParams(),并以super.onMeasure()的调用结束。 例如: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int parentWidth = MeasureSpec.getSize(widthMeasureSpec); int parentHeight = MeasureSpec.getSize(heightMeasureSpec); this.setMeasuredDimension(parentWidth/2, parentHeight); this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight)); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 那么哪一个是正确的? 这两种方法都没有为我工作100%。 我想我真正要问的是没有人知道解释onMeasure(),布局,子视图的维度的教程?

java.lang.IllegalStateException:指定的子项已经有父项

当我第一次实例化片段时,我正在使用片段。 但第二次我得到这个例外。 我无法find我得到错误的路线? 04-04 08:51:54.320: E/AndroidRuntime(29713): FATAL EXCEPTION: main 04-04 08:51:54.320: E/AndroidRuntime(29713): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addViewInner(ViewGroup.java:3013) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2902) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2859) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2839) 04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.NoSaveStateFrameLayout.wrap(Unknown Source) […]

限制Android上ListView的高度

我想在ListView下显示一个button。 问题是,如果ListView得到扩展(项目添加…),button被推出屏幕。 我尝试了一个带权重的LinearLayout (在Android中build议:为什么没有maxHeight的视图? ),但是我得到的权重错了,或者根本不起作用。 另外,我在某处发现了使用RelativeLayout的提示。 然后ListView将被设置在android:layout_above参数的buttonandroid:layout_above 。 问题是我不知道如何定位button。 在我发现的例子中, ListView下面的View使用android:layout_alignParentBottom进行了调整,但是我不希望我的button粘在屏幕的底部。 任何想法,除了使用setHeight方法和一些计算所需的空间? 编辑: 我有很多有用的答案。 bigstone的&user639183的解决scheme几乎完美的工作。 但是,我不得不在button的底部添加一个额外的填充/边距,因为它仍然会被挤出屏幕的一半(但是随后停止) 如果你想把button固定在屏幕的底部,Adinia的回答只有相对的布局。 这不是我的意图,但仍然可能对其他人有用。 AngeloS的解决scheme是我最终select的解决scheme,因为它创造了我想要的效果。 不过,我对button周围的LinearLayout做了两处小小的修改: 首先,因为我不想在我的布局中有任何绝对值,所以我将android:layout_height="45px"更改为wrap_content ,这也很好。 其次,因为我想要水平居中的button,只有垂直的LinearLayout支持,我改变了android:orientation =“horizo​​ntal”为“vertical”。 AngeloS还在他的初始文章中表示,他不确定在ListView周围的LinearLayout的android:layout_weight="0.1"参数是否有任何影响; 我只是尝试,实际上呢! 没有,button被再次推出屏幕。

如何使用isInEditMode()在编辑器中查看具有自定义视图的布局

我必须编辑一个具有自定义视图的软件,当我尝试编辑布局xml时,Eclipse对我说: 在Eclipse中显示时,在自定义视图中使用View.isInEditMode()可以跳过代码 但我不知道如何以及在哪里必须在应用程序中使用isInEditMode() 我的XML文件是 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" > <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:textSize="32dip" android:scrollbars="none" android:lines="1" android:freezesText="true" android:textColor="@color/result" /> <EditText android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:textSize="28dip" android:scrollbars="none" android:singleLine="true" android:autoText="false" android:imeOptions="flagNoEnterAction|flagNoExtractUi" /> <ListView android:id="@+id/history" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:cacheColorHint="#ff000000" android:choiceMode="singleChoice" android:scrollbarStyle="outsideInset" android:scrollbars="none" /> <calculator.GraphView android:id="@+id/graph" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:visibility="gone" /> <include layout="@layout/keyboard" […]