如何从xml获取垂直按钮视图 [英] How get Vertical Button view from xml
问题描述
我想在Vertically中创建一个Button.也许我们可以通过扩展按钮并将画布重新渲染(旋转)到Vertical我们可以得到自定义的Button.但是我需要从xml中获取它.请检查图形表示形式.我需要像这样的按钮.
请参阅下面的链接,应该有解决您的问题的方法
http://blog.stylingandroid.com/archives/796
这是有关如何创建vertical-textView的教程 但是由于Button类扩展了TextView,因此本教程也适用于Buttons
更新:
1)在res/values/styles.xml中创建样式,稍后我们将使用它:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="verticalTextStyle"
parent="android:Widget.TextView">
<item name="android:padding">20dp</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
2)将一个名为hello的默认字符串替换为res/values/strings.xml中的一个命名文本:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="text">Vertical Text</string>
<string name="app_name">VerticalText</string>
</resources>
3)创建自定义VerticalTextView类
public class VerticalTextView extends TextView
{
final boolean topDown;
public VerticalTextView( Context context,
AttributeSet attrs )
{
super( context, attrs );
final int gravity = getGravity();
if ( Gravity.isVertical( gravity )
&& ( gravity & Gravity.VERTICAL_GRAVITY_MASK )
== Gravity.BOTTOM )
{
setGravity(
( gravity & Gravity.HORIZONTAL_GRAVITY_MASK )
| Gravity.TOP );
topDown = false;
}
else
{
topDown = true;
}
}
@Override
protected void onMeasure( int widthMeasureSpec,
int heightMeasureSpec )
{
super.onMeasure( heightMeasureSpec,
widthMeasureSpec );
setMeasuredDimension( getMeasuredHeight(),
getMeasuredWidth() );
}
@Override
protected void onDraw( Canvas canvas )
{
TextPaint textPaint = getPaint();
textPaint.setColor( getCurrentTextColor() );
textPaint.drawableState = getDrawableState();
canvas.save();
if ( topDown )
{
canvas.translate( getWidth(), 0 );
canvas.rotate( 90 );
}
else
{
canvas.translate( 0, getHeight() );
canvas.rotate( -90 );
}
canvas.translate( getCompoundPaddingLeft(),
getExtendedPaddingTop() );
getLayout().draw( canvas );
canvas.restore();
}
}
4)更改res/layout/main.xml中的主布局,使其包含VerticalTextView控件:
<com.stylingandroid.verticaltext.VerticalTextView
style="@style/verticalTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:text="@string/text" />
5)结果
来源: http://blog.stylingandroid.com/archives/796 >
I want create a Button in Vertically.May be we can by extending a button and re-render(rotate) the canvas to vertical we can get the custom Button. But i need it from xml.check the graphical representation.i need a button like this.
Please, see link below, there should be solution for your problem
http://blog.stylingandroid.com/archives/796
This is tutorial about how to create vertical-textView But because of Button class extends TextView this tutorial should work for Buttons too
UPDATE:
1) Create a style in res/values/styles.xml which we’l use later:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="verticalTextStyle"
parent="android:Widget.TextView">
<item name="android:padding">20dp</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
2) replace the default string named hello with one named text to res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="text">Vertical Text</string>
<string name="app_name">VerticalText</string>
</resources>
3) Create custom VerticalTextView class
public class VerticalTextView extends TextView
{
final boolean topDown;
public VerticalTextView( Context context,
AttributeSet attrs )
{
super( context, attrs );
final int gravity = getGravity();
if ( Gravity.isVertical( gravity )
&& ( gravity & Gravity.VERTICAL_GRAVITY_MASK )
== Gravity.BOTTOM )
{
setGravity(
( gravity & Gravity.HORIZONTAL_GRAVITY_MASK )
| Gravity.TOP );
topDown = false;
}
else
{
topDown = true;
}
}
@Override
protected void onMeasure( int widthMeasureSpec,
int heightMeasureSpec )
{
super.onMeasure( heightMeasureSpec,
widthMeasureSpec );
setMeasuredDimension( getMeasuredHeight(),
getMeasuredWidth() );
}
@Override
protected void onDraw( Canvas canvas )
{
TextPaint textPaint = getPaint();
textPaint.setColor( getCurrentTextColor() );
textPaint.drawableState = getDrawableState();
canvas.save();
if ( topDown )
{
canvas.translate( getWidth(), 0 );
canvas.rotate( 90 );
}
else
{
canvas.translate( 0, getHeight() );
canvas.rotate( -90 );
}
canvas.translate( getCompoundPaddingLeft(),
getExtendedPaddingTop() );
getLayout().draw( canvas );
canvas.restore();
}
}
4) change our main layout in res/layout/main.xml to include a VerticalTextView control:
<com.stylingandroid.verticaltext.VerticalTextView
style="@style/verticalTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:text="@string/text" />
5) Result
Source: http://blog.stylingandroid.com/archives/796
这篇关于如何从xml获取垂直按钮视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!