覆盖onMeasure()的正确方法是什么?我见过各种方法.例如,Professional Android Development使用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);
}
Run Code Online (Sandbox Code Playgroud)
另一方面,根据这篇文章,"正确"的方法是使用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);
}
Run Code Online (Sandbox Code Playgroud)
那么哪种方式正确?这两种方法都没有对我有效.
我想我真正想要的是有没有人知道一个解释onMeasure(),布局,子视图尺寸等的教程?
我已经看到了一些SO问题,他们提供了一些可能的方法来实现我想要的.例如:
colorControlHighlight在styles.xml中使用属性.
这是我的styles-v21.xml:
<style name="SelectableItemBackground">
<item name="android:colorControlHighlight">#5677FC</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我的小部件:
<TextView
android:id="@+id/tv_take_photo_as_bt"
android:layout_width="280dp"
android:layout_height="48dp"
android:text="@string/act_take_photo"
style="@style/SelectableItemBackground"/>
Run Code Online (Sandbox Code Playgroud)
它不起作用.我还尝试添加parent="Theme.AppCompat到"SelectableItemBackground"样式,或者更改为colorControlHighlight(no android: prefix)"或更改为?android:attr/selectableItemBackground,既没有用.
backgroundTint在布局中使用属性.
所以我加入android:backgroundTint="#5677FC"我的TextView.还是没用.然后我试着改变android:backgroundTintMode,src_in并且src_atop,他们永远不会有所作为.
那么,当我?attr/selectableItemBackground用作背景时,如何改变波纹颜色.我只关注棒棒糖及以上.先感谢您!
android android-appcompat android-layout material-design android-5.0-lollipop
我正在查看Honeycomb Gallery示例代码(此处),并在尝试在我自己的应用程序中添加操作项时遇到以下代码:
<item android:id="@+id/camera"
android:title="Camera"
android:icon="?attr/menuIconCamera"
android:showAsAction="ifRoom" />
Run Code Online (Sandbox Code Playgroud)
这?attr让我陷入了困境.有人可以解释一下这是做什么的吗?这与抽象有什么关系?我似乎无法在Google上找到任何有用的信息.还有一个列表或属性库我们可以用于图标,而不仅仅是menuIconCamera?
谢谢
编辑:我做了一些调查,发现attrs.xml看起来像这样:
<resources>
<declare-styleable name="AppTheme">
<attr name="listDragShadowBackground" format="reference" />
<attr name="menuIconCamera" format="reference" />
<attr name="menuIconToggle" format="reference" />
<attr name="menuIconShare" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这让我更加困惑.这是做什么的?
我正在尝试将分隔符添加到水平线性布局但是无处可去.分频器没有显示.我是Android的新手.
这是我的布局XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/llTopBar"
android:orientation="horizontal"
android:divider="#00ff00"
android:dividerPadding="22dip"
android:showDividers="middle"
>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="asdf" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="asdf"
/>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 我有一个相当复杂(不是真正的)xml布局文件.其中一个视图是v1带有两个子节点的LinearLayout():EditText(v2)和另一个LinearLayout(v3).子LinearLayout又有一个EditText(v4)和一个ImageView(v5).
对于EditText v2,我将imeOptions作为
android:imeOptions="actionNext"
Run Code Online (Sandbox Code Playgroud)
但是,当我运行应用程序时,键盘return不会检查next,我希望它更改为next.我该如何解决这个问题?
此外,当用户点击下一步时,我希望焦点转到EditText v4.我这样做吗?
对于那些真正需要查看代码的人:
<LinearLayout
android:id="@+id/do_txt_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/col6"
android:orientation="vertical"
android:visibility="gone" >
<EditText
android:id="@+id/gm_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/coldo_text"
android:hint="@string/enter_title"
android:maxLines="1"
android:imeOptions="actionNext"
android:padding="5dp"
android:textColor="pigc7"
android:textSize="ads2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/rev_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/coldo_text"
android:hint="@string/enter_msg"
android:maxLines="2"
android:padding="5dp"
android:textColor="pigc7"
android:textSize="ads2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="@drawable/colbtn_r”
android:clickable="true"
android:onClick=“clickAct”
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:src="@drawable/abcat” />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 有没有方法可以将snackBar的布局更改为自定义View?
现在它变黑了,我们可以改变背景颜色.但我不知道正确的方法来膨胀新的布局并使其成为snackBars背景?
谢谢...
android android-custom-view android-layout snackbar android-snackbar
我想添加ProgressBar上的顶部Button(均为内ConstraintLayout).
<Button
android:id="@+id/sign_in_button"
android:layout_width="280dp"
android:layout_height="75dp"
android:layout_marginBottom="75dp"
android:layout_marginTop="50dp"
android:text="@string/sign_in"
android:textColor="@color/white"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordEditText"
app:layout_constraintVertical_bias="0.0"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@+id/sign_in_button"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/sign_in_button"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.5"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="@+id/sign_in_button"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="@+id/sign_in_button"/>
Run Code Online (Sandbox Code Playgroud)
但即使在呼吁bringToFront进入ProgressBar后onCreate,它总是留在后面Button.
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
progressBar.bringToFront();
Run Code Online (Sandbox Code Playgroud) 我是开发人员.我需要实现如下所示的设计.我已经有功能的应用程序,但想知道如何处理这个?特别是,我对如何在标签下显示"新"项数量感兴趣.我知道怎么做 - 用红点创建新图标,只在有新东西时显示它们.
但是我不知道如何让这些圆圈漂浮在标题之上并在里面显示数字.有人建议过什么吗?样品?路线?
关于分离活动的第二个问题 我应该控制组合这样的按钮,只是在活动上充气吗?否则我可能会创建选项卡式活动,但我不确定是否可以设置它以使其看起来像这样.

我定义了一个类:
public class TestMyFrameLayout extends FrameLayout{
Paint mPaint;
public TestMyFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestMyFrameLayout(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(50f, 50f, 30, mPaint);
}
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
TestMyFrameLayout myFrameLayout = new TestMyFrameLayout(this);
LayoutParams myFrameLayoutParams = new LayoutParams(300,300);
myFrameLayout.setLayoutParams(myFrameLayoutParams);
setContentView(myFrameLayout);
Run Code Online (Sandbox Code Playgroud)
但实际上TestMyFrameLayout.onDraw(Canvas canvas)函数没有被调用,为什么呢?
在使用Android设计支持库TextInputLayout将浮动标签放置在EditText组件上方之后,我想知道是否有办法向组件添加浮动标签Spinner(不一定使用设计库).
通过这个,我的意思是TextView放在上面Spinner(显然没有像这样的动画TextInputLayout),但我希望文本大小,字体和颜色与TextInputLayout浮动标签的匹配.
例如,它看起来像这样(参见Spinners 上方的标签):
正如我之前提到的,我的主要目标是在上面有一个标签Spinner,就像在TextInputLayout- 所以文字大小,字体,颜色和标签与组件之间的距离是相同的.
在有关浮动标签文本字段的Google设计页面上,有一个图表显示了标签相对于组件的尺寸,但没有标签文字颜色或尺寸的指示:
总而言之,我要问:
- 如果有一个特殊的组件来实现我的要求或我可以使用的自定义视图,它会是什么,以及如何使用它.
-如果不是,什么是浮动标签文字大小,颜色和字体,这样我就可以把一个TextView上面我Spinner用上面的图片中显示的布局尺寸.
根据文本字段的Google设计指南,浮动标签有以下内容:
提示和输入字体:Roboto Regular 16sp
Label字体:Roboto Regular 12sp
平铺高度:72dp
文本顶部和底部填充:16dp
文本字段分隔符填充:8dp
以及上面显示的图像.
所以浮动标签字体是:Roboto Regular 12sp.因此,您可以使用a TextView来显示Spinner标签,因为我不知道View您可以使用的任何自定义或特殊组件.
但是,在尝试之后,它看起来并不像图像中显示的那样好.一个自定义视图可能是为了这个美好的,因为它可能看起来更好,但上面的解决方案是实现东西接近我本来想的只是一种方法.
android android-layout android-spinner material-design android-textinputlayout