欢迎大家来到IT世界,在知识的湖畔探索吧!
一、声明控件
1、如果你打开Android Studio后没有自动打开上一次创建的项目(比如我上次创建的是MyFirst App),看到的是图1所示的欢迎界面,直接找到项目点击打开即可
2、打开后如图2,默认展示的是MainActivity.java,我们切换到activity_main.xml中,可以按住ctrl键(Mac系统是Command键),然后鼠标单击第12行的“activity_main”;也可以通过左侧导航栏 res->layout->activity_main.xml双击打开
3、切换后如图3,先看右上方框起来的部分,Android Studio布局支持三种设计模式,纯视图(就是现在看到这种)、视图+代码、纯代码,在纯视图模式下,我们可以直接从右侧控件区拖动控件到页面,可以实时的、直观的看到效果,通过拖动调整间距和位置,个人其实更喜欢split模式,即视图+代码,在通过代码添加控件的同时也能实时看到效果,我们点击split切换到视图+代码模式
4、好,在图4中,粗糙的讲一下,这里系统默认的约束布局(ConstraintLayout),个人习惯使用线性布局(LinearLayout)和相对布局(RelativeLayout),等我改一下
5、改动后的代码如下,
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Word"
android:textSize="22sp"/>
<Button
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"/>
</LinearLayout>
欢迎大家来到IT世界,在知识的湖畔探索吧!
- 根结点下,我添加了“android:orientation=”vertical””,表示垂直方向布局,不添加这一句默认水平方向布局,即“android:orientation=”horizontal””
- 根结点下,添加了“android:gravity=”center””,表示布局中的控件居中
- 为TextView添加了id,并删除了末尾四个属性,因为那四个属性在约束布局下才生效
- 为TextView设置了默认显示文本和字体大小
- 添加了Button控件并设置了id
二、绑定控件
接下来我们切换回MainActivity.java,然后定义和绑定好控件
欢迎大家来到IT世界,在知识的湖畔探索吧!public class MainActivity extends AppCompatActivity {
private TextView tvText;
private Button btnChange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tvText=findViewById(R.id.tv_text);
btnChange=findViewById(R.id.btn_change);
}
}
这里我定义了一个initView方法,并在方法体内进行的控件绑定;一是编码习惯,二是为了避免onCreate方法内看起来太杂太乱
三、通过点击按钮改变控件的显示文本
同理,添加setListener方法,设置btnChange的点击事件
private void setListener() {
btnChange.setOnClickListener((v)->{
tvText.setText("我是点击后的文本");
});
}
别忘了在onCreate()方法里调用,即在“initView();”后添加“setListener();”;
四、TextView的其它属性
俗话说,我可以不用,但你必须得有,这不,TextView支持的属性我理解该有的它都有了,官网对于TextView属性介绍如下:
|
Whether undo should be allowed for editable text. |
android:autoLink |
Controls whether links such as urls and email addresses are automatically found and converted to clickable links. |
android:autoSizeMaxTextSize |
The maximum text size constraint to be used when auto-sizing text. |
android:autoSizeMinTextSize |
The minimum text size constraint to be used when auto-sizing text. |
android:autoSizePresetSizes |
Resource array of dimensions to be used in conjunction with autoSizeTextType set to uniform. |
android:autoSizeStepGranularity |
Specify the auto-size step size if autoSizeTextType is set to uniform. |
android:autoSizeTextType |
Specify the type of auto-size. |
android:autoText |
If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors. |
android:breakStrategy |
Break strategy (control over paragraph layout). |
android:bufferType |
Determines the minimum type that getText() will return. |
android:capitalize |
If set, specifies that this TextView has a textual input method and should automatically capitalize what the user types. |
android:cursorVisible |
Makes the cursor visible (the default) or invisible. |
android:digits |
If set, specifies that this TextView has a numeric input method and that these specific characters are the ones that it will accept. |
android:drawableBottom |
The drawable to be drawn below the text. |
android:drawableEnd |
The drawable to be drawn to the end of the text. |
android:drawableLeft |
The drawable to be drawn to the left of the text. |
android:drawablePadding |
The padding between the drawables and the text. |
android:drawableRight |
The drawable to be drawn to the right of the text. |
android:drawableStart |
The drawable to be drawn to the start of the text. |
android:drawableTint |
Tint to apply to the compound (left, top, etc.) drawables. |
android:drawableTintMode |
Blending mode used to apply the compound (left, top, etc.) drawables tint. |
android:drawableTop |
The drawable to be drawn above the text. |
android:editable |
If set, specifies that this TextView has an input method. |
android:editorExtras |
Reference to an <input-extras> XML resource containing additional data to supply to an input method, which is private to the implementation of the input method. |
android:elegantTextHeight |
Elegant text height, especially for less compacted complex script text. |
android:ellipsize |
If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle. |
android:ems |
Makes the TextView be exactly this many ems wide. |
android:enabled |
Specifies whether the widget is enabled. |
android:fallbackLineSpacing |
Whether to respect the ascent and descent of the fallback fonts that are used in displaying the text. |
android:firstBaselineToTopHeight |
Distance from the top of the TextView to the first text baseline. |
android:fontFamily |
Font family (named by string or as a font resource reference) for the text. |
android:fontFeatureSettings |
Font feature settings. |
android:fontVariationSettings |
Font variation settings. |
android:freezesText |
If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. |
android:gravity |
Specifies how to align the text by the view’s x- and/or y-axis when the text is smaller than the view. |
android:height |
Makes the TextView be exactly this tall. |
android:hint |
Hint text to display when the text is empty. |
android:hyphenationFrequency |
Frequency of automatic hyphenation. |
android:imeActionId |
Supply a value for EditorInfo.actionId used when an input method is connected to the text view. |
android:imeActionLabel |
Supply a value for EditorInfo.actionLabel used when an input method is connected to the text view. |
android:imeOptions |
Additional features you can enable in an IME associated with an editor to improve the integration with your application. |
android:includeFontPadding |
Leave enough room for ascenders and descenders instead of using the font ascent and descent strictly. |
android:inputMethod |
If set, specifies that this TextView should use the specified input method (specified by fully-qualified class name). |
android:inputType |
The type of data being placed in a text field, used to help an input method decide how to let the user enter text. |
android:justificationMode |
Mode for justification. |
android:lastBaselineToBottomHeight |
Distance from the bottom of the TextView to the last text baseline. |
android:letterSpacing |
Text letter-spacing. |
android:lineBreakStyle |
Indicates the line break strategies can be used when calculating the text wrapping. |
android:lineBreakWordStyle |
Specify the phrase-based line break can be used when calculating the text wrapping. |
android:lineHeight |
Explicit height between lines of text. |
android:lineSpacingExtra |
Extra spacing between lines of text. |
android:lineSpacingMultiplier |
Extra spacing between lines of text, as a multiplier. |
android:lines |
Makes the TextView be exactly this many lines tall. |
android:linksClickable |
If set to false, keeps the movement method from being set to the link movement method even if autoLink causes links to be found. |
android:marqueeRepeatLimit |
The number of times to repeat the marquee animation. |
android:maxEms |
Makes the TextView be at most this many ems wide. |
android:maxHeight |
Makes the TextView be at most this many pixels tall. |
android:maxLength |
Set an input filter to constrain the text length to the specified number. |
android:maxLines |
Makes the TextView be at most this many lines tall. |
android:maxWidth |
Makes the TextView be at most this many pixels wide. |
android:minEms |
Makes the TextView be at least this many ems wide. |
android:minHeight |
Makes the TextView be at least this many pixels tall. |
android:minLines |
Makes the TextView be at least this many lines tall. |
android:minWidth |
Makes the TextView be at least this many pixels wide. |
android:numeric |
If set, specifies that this TextView has a numeric input method. |
android:password |
Whether the characters of the field are displayed as password dots instead of themselves. |
android:phoneNumber |
If set, specifies that this TextView has a phone number input method. |
android:privateImeOptions |
An addition content type description to supply to the input method attached to the text view, which is private to the implementation of the input method. |
android:scrollHorizontally |
Whether the text is allowed to be wider than the view (and therefore can be scrolled horizontally). |
android:selectAllOnFocus |
If the text is selectable, select it all when the view takes focus. |
android:shadowColor |
Place a blurred shadow of text underneath the text, drawn with the specified color. |
android:shadowDx |
Horizontal offset of the text shadow. |
android:shadowDy |
Vertical offset of the text shadow. |
android:shadowRadius |
Blur radius of the text shadow. |
android:singleLine |
Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key. |
android:text |
Text to display. |
android:textAllCaps |
Present the text in ALL CAPS. |
android:textAppearance |
Base text color, typeface, size, and style. |
android:textColor |
Text color. |
android:textColorHighlight |
Color of the text selection highlight. |
android:textColorHint |
Color of the hint text. |
android:textColorLink |
Text color for links. |
android:textCursorDrawable |
Reference to a drawable that will be drawn under the insertion cursor. |
android:textFontWeight |
Weight for the font used in the TextView. |
android:textIsSelectable |
Indicates that the content of a non-editable text can be selected. |
android:textScaleX |
Sets the horizontal scaling factor for the text. |
android:textSelectHandle |
Reference to a drawable that will be used to display a text selection anchor for positioning the cursor within text. |
android:textSelectHandleLeft |
Reference to a drawable that will be used to display a text selection anchor on the left side of a selection region. |
android:textSelectHandleRight |
Reference to a drawable that will be used to display a text selection anchor on the right side of a selection region. |
android:textSize |
Size of the text. |
android:textStyle |
Style (normal, bold, italic, bold|italic) for the text. |
android:typeface |
Typeface (normal, sans, serif, monospace) for the text. |
android:width |
Makes the TextView be exactly this wide. |
五、Button支持的其它属性
官网对于Button的属性没有列出,因为它本质上就是一个TextView,只不过封装了一下
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/17957.html