2014-09-02 121 views
0

我有一個正常的相對佈局,文本視圖和進度條。Android擴展和佈局合同動畫

現在,因爲我有一個固定的寬度和高度的佈局文本正確地放置在中心和看起來不錯,佈局onclick我們正在改變進度欄的可見性,以「可見」,但因爲我有一個固定寬度的進度條位於文本的頂部。

我想要實現的是,onclick和動畫一起增加布局的右端寬度。

這裏是我的代碼:佈局

<RelativeLayout 
     android:id="@+id/rellyt" 
     android:layout_width="150dp" 
     android:layout_height="35dp" 
     android:layout_margin="5dp" 
     android:background="#B7E4FF" 
     android:clickable="true" > 

     <TextView 
      android:id="@+id/txt" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerInParent="true" 
      android:text="click on this button" 
      android:textColor="#000000" 
      android:textSize="14sp" /> 

     <ProgressBar 
      android:id="@+id/prgbar" 
      style="@android:style/Widget.ProgressBar.Small" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="8dp" 
      android:visibility="visible" /> 
    </RelativeLayout> 

屏幕截圖:

enter image description here

+0

機器人:layout_gravity =浮動在CSS 安卓重力=在CSS文本對齊...............使用這些屬性來設置文本查看,因爲當你動畫..你的textView將取代.. – Nepster 2014-09-02 08:00:03

+0

@Nepster你能詳細說明嗎? – Goofy 2014-09-02 08:18:07

+0

右端寬度意味着你必須增加你的RelativeLayout的填充權。 – Nepster 2014-09-02 08:23:40

回答

0

試試這個辦法,希望這將幫助你解決你的問題。

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <LinearLayout 
     android:id="@+id/customLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:background="#B7E4FF" 
     android:gravity="center" 
     android:padding="5dp"> 

     <TextView 
      android:id="@+id/txt" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="click on this button" 
      android:textColor="#000000" 
      android:textSize="14sp" /> 

     <ProgressBar 
      android:id="@+id/prgbar" 
      style="@android:style/Widget.ProgressBar.Small" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:visibility="invisible" /> 
    </LinearLayout> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 

    private LinearLayout customLayout; 
    private ProgressBar prgbar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     customLayout = (LinearLayout) findViewById(R.id.customLayout); 
     prgbar = (ProgressBar) findViewById(R.id.prgbar); 
     customLayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(prgbar.getVisibility() == View.INVISIBLE){ 
        prgbar.setVisibility(View.VISIBLE); 
       }else{ 
        prgbar.setVisibility(View.INVISIBLE); 
       } 
      } 
     }); 

    } 
} 
+0

我試過你的代碼,第一件事是我需要動畫,因爲我已經解釋,第二件事是文本應該在中心和左端 – Goofy 2014-09-02 07:35:58

1

動畫

public class ResizeWidthAnimation extends Animation 
{ 
    private int mWidth; 
    private int mStartWidth; 
    private View mView; 

    public ResizeWidthAnimation(View view, int width) 
    { 
     mView = view; 
     mWidth = width; 
     mStartWidth = view.getWidth(); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) 
    { 
     int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime); 

     mView.getLayoutParams().width = newWidth; 
     mView.requestLayout(); 
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, int parentHeight) 
    { 
     super.initialize(width, height, parentWidth, parentHeight); 
    } 

    @Override 
    public boolean willChangeBounds() 
    { 
     return true; 
    } 
} 

使用

if(animate) 
{ 
    ResizeWidthAnimation anim = new ResizeWidthAnimation(leftFrame, leftFragmentWidthPx); 
    anim.setDuration(500); 
    leftFrame.startAnimation(anim); 
} 
else 
{ 
    this.leftFragmentWidthPx = leftFragmentWidthPx; 
    LayoutParams lp = (LayoutParams) leftFrame.getLayoutParams(); 
    lp.width = leftFragmentWidthPx; 
    leftFrame.setLayoutParams(lp); 
} 
+0

是的,我也在尋找這在這裏:http://stackoverflow.com/questions/16641539/animate-change-width-of-android-relativelayout但寬度是從左右兩端增加,我想只增加在右端作爲進展是在右端 – Goofy 2014-09-02 08:28:52

+0

任何想法我怎麼能調整? – Goofy 2014-09-02 08:36:01

+0

而不是寬度,你應該去填充右..它可以解決你的問題 – Nepster 2014-09-02 09:49:22