【问题标题】:Increase Android button's width from right to left从右到左增加Android按钮的宽度
【发布时间】:2015-09-26 14:22:43
【问题描述】:

我想让按钮宽度从右向左增加,而不是从左向右增加,我该怎么办?

例如:

Button btn = findviewbyId(R.id.Button1);
btn.setwidth(1000);

这将从按钮右侧开始增加宽度,但我希望它向相反方向增加。

【问题讨论】:

  • 你的布局 xml 怎么样

标签: android android-layout android-fragments android-intent android-activity


【解决方案1】:

试试这个,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.btns.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="66dp"
    android:gravity="end"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final Button btn=(Button) findViewById(R.id.button2);
    btn.setOnClickListener(new View.OnClickListener() {

        int i=0;
        @Override
        public void onClick(View arg0) {

            i=i+50;

            btn.setWidth(i);

            p(i);

        }
        private void p(int i) {

            System.out.println(i);

        }
    });

}}

【讨论】:

    【解决方案2】:

    我认为您应该设置 gravitylayout_gravity 属性。

    【讨论】:

    • 回复太晚了,但无论如何 android:gravity="right" 工作。
    【解决方案3】:

    获取初始宽度

    int initialwidth;
    Button btn=findviewbyId(R.id.Button1);
    ViewTreeObserver vto = layout.getViewTreeObserver();  
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
        @Override  
        public void onGlobalLayout() {  
            this.btn.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
           initialwidth = btn.getMeasuredWidth();  
        }  
    }); 
    

    然后使用初始宽度和新宽度之间的差异,设置左边距

    LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,      
            LayoutParams.WRAP_CONTENT
    );
    int diff=1000-initialwidth;
    params.setMargins(diff, top, right, bottom);
    btn.setWidth(1000);
    btn.setLayoutParams(params);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-30
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 2011-08-03
      相关资源
      最近更新 更多