Android版 五星红旗,android版五星红旗
投稿于 被查看 4483 次 评论:30
Android版 五星红旗,android版五星红旗
<无详细内容>1.代码
import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.widget.Button; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class StarActivity extends Activity { Button[] s = new Button[5]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); LinearLayout linearLayout = (LinearLayout)findViewById(R.id.ll); LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, (metric.widthPixels * 2) / 3); linearLayout.setLayoutParams(llParams); RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); for (int i = 0; i < 5; i++) { s[i] = new Button(this); s[i].setId(i + 1); s[i].setText("★"); if (i == 0) s[i].setTextSize(70); else s[i].setTextSize(20); s[i].setTextColor(0xffffff00); s[i].setBackgroundColor(0x00000000); s[i].setPadding(0, 0, 0, 0); rl.addView(s[i]); } RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) s[1] .getLayoutParams(); if (params1 == null) params1 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params1.addRule(RelativeLayout.RIGHT_OF, s[0].getId()); s[1].setLayoutParams(params1); RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) s[2] .getLayoutParams(); if (params2 == null) params2 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params2.addRule(RelativeLayout.RIGHT_OF, s[1].getId()); params2.addRule(RelativeLayout.BELOW, s[1].getId()); s[2].setLayoutParams(params2); RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) s[3] .getLayoutParams(); if (params3 == null) params3 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params3.addRule(RelativeLayout.RIGHT_OF, s[1].getId()); params3.addRule(RelativeLayout.BELOW, s[2].getId()); s[3].setLayoutParams(params3); RelativeLayout.LayoutParams params4 = (RelativeLayout.LayoutParams) s[4] .getLayoutParams(); if (params4 == null) params4 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params4.addRule(RelativeLayout.RIGHT_OF, s[0].getId()); params4.addRule(RelativeLayout.BELOW, s[3].getId()); s[4].setLayoutParams(params4); } }
2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff0000" > <RelativeLayout android:id="@+id/rl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" > </RelativeLayout> </LinearLayout> </LinearLayout>
用户评论