我正在尝试使用RelativeLayout和一个RadioGroup创建一个标签栏,该组有一个指示器,该指示器将滑动以指示RadioGroup中的活动RadioButton.
这些是定制的单选按钮,它们是有效的矩形,文本上有一个图标,所有内容都居中,它使用自定义状态选择器作为背景.
这是我目前的等级制度:
<RelativeLayout> // Main view of the tab bar
<RadioGroup> // The buttons
<RadioButton />
<RadioButton />
<RadioButton />
</RadioGroup>
<ImageView /> // The indicator
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我的想法是单击一个单选按钮时,我会将指示器对齐到所选单选按钮的顶部和底部(并为其设置动画).但是,它似乎layout_align<Edge>
只适用于兄弟元素,而不适用于另一个视图组的成员,即我可以与RadioGroup本身对齐,但不能与其中的RadioButton对齐.
我想把指标作为RadioGroup的成员,但由于RadioGroup是LinearLayout的扩展,似乎没有办法将它放在给定RadioButton的边缘.
谁能想到我怎么解决这个问题?或者也许比我的animate-align-to-button技术有更好的解决方案?
更新 使用@superjos帮助,我设法很好地解决了这个问题.
我必须给每个按钮一个已知的高度而不是使用wrap_content
(不理想,但对于这个项目,它可能应该工作正常).使指示器高度与按钮的高度相匹配.确保将RadioGroup设置为包装内容并在父视图中垂直居中.将指标设置为alignTop
和toRightOf
RadioGroup.然后,对于按钮单击侦听器:
int prevY, newY;
int prevButtonIndex, newButtonIndex;
public void moveIndicator(button, indicator, index) {
prevY = newY;
newY = prevY + (index - prevButtonIndex) * button.getHeight();
TranslateAnimation animation = new TranslateAnimation(0, 0, prevY, newY);
animation.setDuration(500);
animation.setFillAfter(true); // stay at final …
Run Code Online (Sandbox Code Playgroud) 我动态地添加和删除视图到自定义视图(FrameLayout)执行此操作:
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMyView = inflater.inflate(resId, this, true);
Run Code Online (Sandbox Code Playgroud)
稍后我尝试删除视图,但视图不会删除:
removeView(mMyView);
Run Code Online (Sandbox Code Playgroud)
如果我这样做,一切都按预期工作:
mMyView = inflater.inflate(resId, this, **false**);
addView(mMyView);
Run Code Online (Sandbox Code Playgroud)
唯一的区别是我手动添加视图而不是让inflate调用.有谁知道为什么这会有所作为?
在我的布局中,我有一个像这样的结构:
--RelativeLayout
|
--FrameLayout
|
--Button, EditText...
Run Code Online (Sandbox Code Playgroud)
我想在RelativeLayout和FrameLayout中处理触摸事件,所以我在这两个视图组中设置了onTouchListener.但只捕获RelativeLayout中的触摸.
为了尝试解决这个问题,我编写了自己的CustomRelativeLayout,并覆盖了onInterceptTouchEvent
,现在捕获了子ViewGroup(FrameLayout)中的点击,但按钮和其他视图中的单击没有任何效果.
在我自己的自定义布局中,我有这个:
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建的Activity类型的一个很好的例子是gmail应用程序设置Activity.它会垂直列出所有可自定义的设置,每个设置之间都有水平规则.通常看起来相同的领域有很多种.例如:文本字段通常显示带有下面描述的粗体标题和右边带圆圈的三角形.
根视图是什么类型的视图组?每个字段都有哪种视图或视图组?
我试图将xml布局扩展为自定义ViewGroup
.在给布局膨胀之后,ViewGroup
只显示布局的根视图,实际上它应该将完整的布局文件显示到ViewGroup
.
我在stackoverflow和其他网站上经历了与此相关的其他类似问题,但没有帮助.
这是我的自定义代码ViewGroup
:
public class ViewEvent extends ViewGroup {
private final String LOG_TAG="Month View Event";
public ViewEvent(Context context) {
super(context);
}
public ViewEvent(Context context,AttributeSet attrs) {
super(context,attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount= getChildCount();
for(int i=0;i<childCount;i++)
{
Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b); …
Run Code Online (Sandbox Code Playgroud) 我有一个 ViewGroup 的奇怪错误。对于我的主视图,我在此链接中使用类ViewFlow 项目
java.lang.IllegalArgumentException: parameter must be a descendant of this view
at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:4153)
at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:4090)
at android.view.ViewRootImpl.scrollToRectOrFocus(ViewRootImpl.java:2129)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:1849)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1641)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2449)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 Intent 或 Finish() 返回此视图时会发生此错误;另一个视图中的方法。当我使用后退按钮时,这不是问题。
所以我不知道为什么我有这个错误。谢谢你的帮助。
我认为标题涵盖了它:)我有一个可绘制对象,可以获取 .png 照片,我可以用手势操作它。我还有一个 xml 布局,其中的背景图像应该位于该可绘制对象的后面。一切都发生在一个片段中。
当我运行代码并到达此片段时,会显示 png 并且手势可以工作,但是,没有膨胀的布局并且按下后退按钮时应用程序崩溃(我猜这是因为我在片段中使用 setContentView 所以有没有返回堆栈?我该如何避免这种情况?)。
稍后我将向场景添加其他图层。
我的问题是,我怎样才能用 xml 布局来膨胀片段,在它上面显示可绘制的,也许稍后在所有这些之上添加其他视图?
该片段的代码如下:
public class RoomFragment extends Fragment {
ViewGroup mRoot;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = (ViewGroup) inflater.inflate(R.layout.room_fragment, null);
/** Placing furniture .png element in SandboxView */
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.furniture);
View view = new SandboxView(this.getActivity(), bitmap);
this.getActivity().setContentView(view); // Replace with inflater?
return mRoot;
}}
Run Code Online (Sandbox Code Playgroud)
谢谢你!
我的 Android 应用程序中有几个具有相同基本结构的活动,我正在尝试使我的布局变干。重复的代码如下所示。它包含一个带有“返回”和“仪表板”链接的页脚的可滚动区域。还有一个 FrameLayout 用于在可滚动区域的顶部应用渐变。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ScrollView
android:layout_width="match_parent"
android:layout_height="689px">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- THE REAL PAGE CONTENT GOES HERE -->
</LinearLayout>
</ScrollView>
<ImageView
android:src="@drawable/GradientBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50px"
android:background="?attr/primaryAccentColor">
<Button
android:layout_width="wrap_content"
android:layout_height="26px"
android:layout_gravity="center_vertical"
local:MvxBind="Click GoBackCommand" />
<Button
android:layout_width="wrap_content"
android:layout_height="26px"
local:MvxBind="Click ShowDashboardHomeCommand" />
</FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
为了去重复我的活动,我认为我需要做的是创建一个从 LinearLayout 继承的自定义 ViewGroup。在该代码中,从 XML 文件加载上述内容。我迷路的是如何让活动中的子内容加载到正确的位置。例如,假设我的 Activity 现在包含:
<com.myapp.ScrollableVerticalLayoutWithDashboard
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- THE REAL PAGE CONTENT GOES HERE -->
<TextView android:text"blah blah …
Run Code Online (Sandbox Code Playgroud) android android-custom-view viewgroup xamarin android-viewgroup
setAlpha
对ViewGroup
API级别14(ICS)-19(KitKat)中的实例没有影响,即使它是在API级别11中引入的.任何想法为什么?
我甚至尝试过使用支持库,但这也不起作用.
例:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup view = (ViewGroup) layoutInflater.inflate(R.layout.my_layout, null);
view.setAlpha(0.5f);
Run Code Online (Sandbox Code Playgroud)
它适用于API级别21+,但不适用于API级别14-19.
PS,我还没有测试过以下API级别14.
有一个带有 wrap_content 布局参数的 LinearLayout。我在里面有几个带有 match_parent 参数的视图。让我们来看看。
情况1:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#004400">
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
案例2:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="What a terrible failure!"
android:background="#009900"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
案例3:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#000099"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是预期的行为。视图已绘制。由于声望低于 10,我无法发布第三张图片。感谢堆栈溢出!:)
所有示例均在真实设备上进行测试,而不仅仅是在 android studio 预览中。
从 android Develop -> API Guides -> User Interface -> Layouts: wrap_content - 告诉您的视图将其自身大小调整为其内容所需的尺寸。
考虑第二种情况。我仍然可以理解按钮有内容,而父视图只接受按钮文本和填充——它是“内容”。
但是第一种情况呢?孩子清楚地说“我想和match_parent一起画”。我知道 RelativeLayout 没有孩子,但它不应该打扰父容器还是应该这样做?
或者,如果Android试图通过请求“内容大小”(OnMeasure)来下到较低级别,那为什么我们需要设置宽度。如果它如此聪明并为我决定一切?
这里到底发生了什么?我不明白。为什么会心动?
android android-layout viewgroup android-relativelayout android-wrap-content