What did I do wrong when setting textview in the following position?
- Linear Layout -
(left)imageview -textview-------------------------------------------- (right)imageview -textview
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
parent.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,convertDpToPixel(23));
textView.setLayoutParams(params);
icFeature.setLayoutParams(params);
icFeature2.setLayoutParams(params);
textView2.setLayoutParams(params);
textView2.setGravity(Gravity.RIGHT);
parent.addView(textView);
parent.addView(icFeature);
parent.addView(icFeature2);
parent.addView(textView2);
linearLayout.addView(parent);
Give below a try. It can be helpful,
RelativeLayout.LayoutParams textViewLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// add a rule to align to the left
textViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//align right
textViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// make sure the rule was applied
textView.setLayoutParams(textViewLayoutParams);
You are setting the TextView's own Gravity attribute which controls how its text renders within its own bounds. You want to set the Gravity attribute on the LayoutParams instead:
https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams#gravity
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
parent.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, convertDpToPixel(23));
textView.setLayoutParams(params);
// Create new params for second view
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, convertDpToPixel(23));
params2.gravity = Gravity.RIGHT | Gravity.END; // Set gravity on layout params
textView2.setLayoutParams(params2);
// textView2.setGravity(Gravity.RIGHT); // Remove this
icFeature.setLayoutParams(params);
icFeature2.setLayoutParams(params);
parent.addView(textView);
parent.addView(icFeature);
parent.addView(icFeature2);
parent.addView(textView2);
linearLayout.addView(parent);
Hope that helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.