2017-04-04 52 views
0

我有CustomLayout其中TextView我設置文本和繪製編程但是問題是當我設置drawable它正在添加一個巨大的填充與我想繪製更多在中心而不是頂部。刪除drawableTop和文本之間的填充

定製的ViewGroup

public class ProfileGridView extends FrameLayout { 
public ProfileGridView(@NonNull Context context) { 
    super(context); 
    initView(context,null); 
} 

public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs); 
    initView(context,attrs); 

} 

public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    initView(context,attrs); 
} 

@TargetApi(21) 
public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    initView(context,attrs); 
} 

public void initView(Context context, AttributeSet attrs) { 
    TextView gridTextView; 
    View view = inflate(context, R.layout.square_grid, null); 
    gridTextView = (TextView) view.findViewById(R.id.grid_description); 

    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProfileGridView); 
    Drawable drawable = array.getDrawable(R.styleable.ProfileGridView_drawble); 
    String description = array.getString(R.styleable.ProfileGridView_grid_description); 

    gridTextView.setText(description); 
    gridTextView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null); 
    addView(view); 
    array.recycle(); 
    } 
} 

CustomView LayoutFile

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/grid_description" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:drawablePadding="8dp" 
android:gravity="center" 
android:padding="0dp" 
android:textSize="20sp" /> 

MainActivit.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <com.example.views.ProfileGridView 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     app:drawble="@drawable/ic_message" 
     app:grid_description="Message"/> 

</RelativeLayout> 

Screenshot of the View

回答

0

你可以使用一個CustomView LayoutFile這樣的繪製?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:gravity="center"> 
<ImageView 
    android:id="@+id/image" 
    android:layout_width="52dp" 
    android:layout_height="52dp" /> 
<TextView 
    android:id="@+id/grid_description" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:padding="4dp" 
    android:textSize="20sp"/> 
</LinearLayout> 

並設置像相反編輯

至於你問的這個

ImageView image; 
    image = (ImageView) view.findViewById(R.id.image); 
    image.setImageDrawable(drawable); 

gridTextView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null); 

提拉,可以使ProfileGridView WRAP_CONTENT而不是200dp。使用setCompoundDrawablesWithIntrinsicBounds將可繪製表單附加到TextView(200dp)的邊界上。 [你可以查看文檔的更多信息](https://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(android.graphics.drawable.Drawable,android.graphics.drawable.Drawable,android.graphics.drawable.Drawable,android.graphics.drawable.Drawable))

+0

我想這是做到這一點的唯一方法,而不是使用LinearLayout,我想用TextView設置ImageDrawable頂部 –

+0

@prathamkesarkar檢查編輯 – jorgeavilae

+0

通過攻擊可繪製的邊界你的意思是將TextView的高度設置爲200dp並將wrap_content添加到它的高度在activity_main –

0

這不是一個填充的問題,是因爲你是你能在繪製文件中使用「WRAP_CONTENT」設置可繪製的寬度和高度爲200dp

如果不只是有一個圖像文件,並將其設置爲TextView的

+0

試試將寬度和高度設置爲40dp或者什麼,你會看到不是填充 –

+0

我試着在textview中設置高度爲64dp,但它仍然向我展示了同樣的東西,它似乎像drawable正在嘗試使用所有可用空間 –

+0

嘗試更改Drawable RelativeLayout寬度也是pls,從match_parent到wrap_content甚至是你說的64dp –