2015-09-26 110 views
0

我需要一个具有纹理背景和动态文本的“按钮”。最终的结果应该是这样,但背景是一张质感的图片。 Custom Button with Text: 我试着用里面的RelativeLayout,但问题是CustomView的结果不是wrap_content的宽度/高度。带有自定义背景的Android TextView?

这里是custom_buttonXML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:background="@drawable/custom_button_selector" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/tv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     /> 
</RelativeLayout> 

这里是Code

public class CustomButton extends RelativeLayout { 

private LayoutInflater mInflater; 

private RelativeLayout mContainer; 

private TextView mTextView; 

public CustomButton(Context context) { 
    super(context); 
    mInflater = LayoutInflater.from(context); 
    customInit(context); 
} 

public CustomButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mInflater = LayoutInflater.from(context); 
    customInit(context); 
} 

public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    mInflater = LayoutInflater.from(context); 
    customInit(context); 
} 

private void customInit(Context ctx) { 
    mContainer = (RelativeLayout) mInflater.inflate(R.layout.cutom_button_view, this, true); 

    mTextView = (TextView) mContainer.findViewById(R.id.tv); 
} 

public RelativeLayout getContainer() { 
    return mContainer; 
} 

public TextView getTextView() { 
    return mTextView; 
} 
} 

这是我如何使用它的那一刻,它工作

<com.myapp.views.CustomButton 
       android:id="@+id/btn" 
       android:layout_width="80dp" 
       android:layout_height="50dp"/> 

但我想使用这样的(动态文本),并将其将无法​​正常工作

<com.myapp.views.CustomButton 
        android:id="@+id/btn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/> 

实际上,它将在整个屏幕上伸展,无视布局规则。 如果这个解决方案是一个坏主意,你能推荐一个能产生相同结果的替代方案吗?

+1

什么错'TextView.setBackground'? – pskink

+0

它只是不会在图片上居中显示文字...我必须使用填充,但它会打破动态文本长度。其实,我的问题的核心是动态文本长度:( – nightfixed

+1

什么是错误的'TextView.setGravity'? – pskink

回答

0

自定义布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/my_background"> 

<TextView 
    android:id="@+id/tv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:gravity="center" /> 
</RelativeLayout> 

自定义布局的java:

public class CustomButton extends FrameLayout { 


private RelativeLayout mContainer; 

private TextView mTextView; 

public CustomButton(Context context) { 
    super(context); 
    customInit(context); 
} 

public CustomButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    customInit(context); 
} 

public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    customInit(context); 
} 

private void customInit(Context ctx) { 
    LayoutInflater.from(ctx).inflate(R.layout.custom_button_viewa, this, true); 
    mContainer = (RelativeLayout) findViewById(R.id.container); 
    mTextView = (TextView) findViewById(R.id.tv); 
} 

public RelativeLayout getContainer() { 
    return mContainer; 
} 

public TextView getTextView() { 
    return mTextView; 
} 
} 

现在可以作为:

<com.androidbolts.databindingsample.model.CustomButton 
     android:id="@+id/customButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
1

你需要的形状文件的..

试试这个。

<shape android:shape="rectangle"> 
    <corners android:radius="56dp" /> 

    <solid android:color="Color Whaterver You Want" /> 

    <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp" /> 
</shape> 

将此xml文件放在res/drawal文件夹下。

现在只是在你看来通过这个属性..

android:background="@drawable/your file name"

跳上它为你..