Align drawable image inside a text view to center

前端 未结 6 1360
挽巷
挽巷 2021-02-19 11:58

I have a textview which holds an image and some text.

programmatically i have added the drawable image to the left side of text view

txtIDFav.setCompound         


        
6条回答
  •  难免孤独
    2021-02-19 12:33

    I assume it is happened because the width of textView is set to match_parent or fill_parent. Try setting textView width to wrap_content and center the whole textView not only text.

    Layout:

    
    
    
        
    
    

    Activity:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView textViewTest=(TextView)findViewById(R.id.textViewTest);
            textViewTest.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_launcher, 0, 0, 0);
        }
    }
    

    Result

提交回复
热议问题