android - क्लिपबोर्ड क्या है
मैं टेक्स्टव्यू में क्षैतिज और लंबवत पाठ कैसे केंद्रित करूं? (20)
TextView गुरुत्वाकर्षण आपके मूल लेआउट के अनुसार काम करता है।
रैखिक लयआउट :
यदि आप लीनियरलाउट का उपयोग करते हैं तो आपको दो गुरुत्वाकर्षण विशेषता एंड्रॉइड मिलेगी: गुरुत्वाकर्षण और एंड्रॉइड: layout_gravity
एंड्रॉइड: गुरुत्वाकर्षण: एंड्रॉइड के दौरान टेक्स्ट व्यू के आंतरिक पाठ की लेआउट औषधि का प्रतिनिधित्व करता है: layout_gravity: पैरेंट व्यू में टेक्स्ट व्यू स्थिति का प्रतिनिधित्व करता है।
यदि आप क्षैतिज और लंबवत केंद्र को टेक्स्ट सेट करना चाहते हैं तो नीचे दिए गए कोड का उपयोग करें
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:background="@android:color/background_light"
android:layout_height="300dp">
<TextView
android:layout_width="match_parent"
android:text="Hello World!"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
/>
</LinearLayout>
सापेक्ष लयआउट :
RelativeLayout का उपयोग करके आप टेक्स्ट व्यू में नीचे संपत्ति का उपयोग कर सकते हैं
एंड्रॉइड: टेक्स्टव्यू में टेक्स्ट सेंटर के लिए गुरुत्वाकर्षण = "केंद्र"।
एंड्रॉइड: गुरुत्वाकर्षण = "center_horizontal" आंतरिक पाठ यदि आप क्षैतिज केंद्रित चाहते हैं।
एंड्रॉइड: गुरुत्वाकर्षण = "center_vertical" आंतरिक पाठ यदि आप लंबवत केंद्रित चाहते हैं।
एंड्रॉइड: layout_centerInParent = "true" अगर आप टेक्स्ट व्यू को मूल दृश्य की केंद्र स्थिति में चाहते हैं। एंड्रॉइड: layout_centerHorizontal = "true" यदि आप टेक्स्ट व्यू को मूल दृश्य के क्षैतिज केंद्र में चाहते हैं। एंड्रॉइड: layout_center वर्टिकल = "सत्य" यदि आप टेक्स्ट व्यू को मूल दृश्य के लंबवत केंद्र में चाहते हैं।
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:background="@android:color/background_light"
android:layout_height="300dp">
<TextView
android:layout_width="match_parent"
android:text="Hello World!"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
/>
</RelativeLayout>
मैं TextView
में क्षैतिज और लंबवत पाठ को कैसे TextView
करूं, ताकि यह Android
में टेक्स्ट TextView
के बीच में दिखाई दे?
आप इसे गतिशील रूप से उपयोग करके भी सेट कर सकते हैं:
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
आप संयोजन का भी उपयोग कर सकते हैं:
android:gravity="left|center"
फिर, यदि टेक्स्टव्यू चौड़ाई "fill_parent" से अधिक है तो टेक्स्ट को अभी भी बाएं से गठबंधन किया जाएगा (केवल गुरुत्वाकर्षण सेट के साथ केंद्रित नहीं है)।
आपको टेक्स्टव्यू ग्रेविटी (सेंटर क्षैतिज और केंद्र वर्टिकल) सेट करने की आवश्यकता है:
android:layout_centerHorizontal="true"
तथा
android:layout_centerVertical="true"
और गतिशील रूप से उपयोग कर रहे हैं:
textview.setGravity(Gravity.CENTER);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
इसे करने के दो तरीके हैं।
एक्सएमएल कोड में पहला। आपको Gravity
विशेषता पर ध्यान देना होगा। आप ग्राफिक संपादक में यह विशेषता भी पा सकते हैं; यह एक्सएमएल संपादक से आसान हो सकता है।
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text"
/>
आपके विशिष्ट परिदृश्य के लिए, गुरुत्वाकर्षण के मूल्य होंगे:
center_vertical|center_horizontal
ग्राफिकल संपादक में आपको सभी संभावित मान मिलेंगे, यहां तक कि उनके परिणाम भी देखेंगे।
एक्सएमएल फ़ाइल में प्रयोग करें।
लेआउट फ़ाइल
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/stringtext"/>
या:
जावा क्लास के अंदर इसका इस्तेमाल करें
TextView textView =(TextView)findViewById(R.id.texviewid);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
जैसा कि ऊपर दिए गए कई उत्तरों का सुझाव है ठीक काम करता है।
android:gravity="center"
यदि आप इसे लंबवत रूप से केन्द्रित करना चाहते हैं:
android:gravity="center_vertical"
या बस क्षैतिज:
android:gravity="center_horizontal"
टेक्स्ट व्यू की ऊंचाई और चौड़ाई सामग्री को लपेटती है, फिर टेक्स्टव्यू के भीतर टेक्स्ट हमेशा केंद्रित होता है, फिर इसका उपयोग करके अपने मूल लेआउट में केंद्र बनाएं:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello.."/>
</RelativeLayout>
LinearLayout के लिए भी कोड समान है:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello.."/>
</LinearLayout>
और प्रो-व्याकरणिक माता-पिता रिश्तेदार लेआउट जावा कोड है, यह रन टाइम पर आपकी गतिविधि में ऐसा कुछ उपयोग करता है
TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);
नीचे दिए गए कोड का उपयोग xml में करें, यह मेरे लिए काम करता है आप केंद्र में होने वाले अभिविन्यास को बदल सकते हैं
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="@+id/id_text"
android:layout_width="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/white"
android:textStyle="normal"
android:layout_height="wrap_content"
android:text="centre me"/>
</LinearLayout>
पाठ केंद्रित करने के लिए आप ऐसा कर सकते हैं
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
मुझे लगता है कि आप एक्सएमएल लेआउट का उपयोग कर रहे हैं।
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/**yourtextstring**"
/>
और @stealthcopter ने जावा में टिप्पणी की: .setGravity(Gravity.CENTER);
मेरा जवाब यह है कि मैंने अपने ऐप में इस्तेमाल किया था। यह स्क्रीन के केंद्र में पाठ दिखाता है।
<TextView
android:id="@+id/txtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/subject"
android:layout_margin="10dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
यदि आप टेबललेआउट का उपयोग कर रहे हैं तो सुनिश्चित करें कि टेबलरो के गुरुत्वाकर्षण को केंद्र में भी सेट करना है। अन्यथा यह काम नहीं करेगा। कम से कम यह मेरे साथ तब तक काम नहीं करता जब तक कि मैं टेबलरो के केंद्र में गुरुत्वाकर्षण सेट नहीं करता।
उदाहरण के लिए, इस तरह:
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">
<TextView android:text="@string/chf" android:id="@+id/tv_chf" android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"></TextView>
</TableRow>
यदि आप टेबललेआउट में टेबलरो पर टेक्स्ट को सेंटर करने का प्रयास कर रहे हैं, तो यहां मैंने यह कैसे हासिल किया है:
<TableRow android:id="@+id/rowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView android:id="@+id/lblSomeLabel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="100"
android:text="Your Text Here" />
</TableRow>
यदि टेक्स्ट TextView's
ऊंचाई और चौड़ाई wrap content
को TextView
तो TextView
भीतर टेक्स्ट हमेशा केंद्रित होता है। लेकिन यदि match_parent
TextView's
चौड़ाई match_parent
और ऊंचाई match_parent
या wrap_content
तो आपको नीचे कोड लिखना होगा:
सापेक्ष लयआउट के लिए:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</RelativeLayout>
LinearLayout के लिए:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</LinearLayout>
रैखिक लेआउट के लिए: एक्सएमएल में इस तरह कुछ उपयोग करें
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text goes here"
/>
रन टाइम पर ऐसा करने के लिए अपनी गतिविधि में ऐसा कुछ उपयोग करें
TextView textView1 =(TextView)findViewById(R.id.texView1);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
सापेक्ष लेआउट के लिए: एक्सएमएल में इस तरह की कुछ चीज़ों का उपयोग करें
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:text="Your Text goes here"
/>
रन टाइम पर ऐसा करने के लिए अपनी गतिविधि में ऐसा कुछ उपयोग करें
TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);
सापेक्ष लेआउट के लिए इसका इस्तेमाल करें
android:layout_centerInParent="true"
और अन्य लेआउट के लिए
android:gravity="center"
RelativeLayout
, यह इसके साथ अच्छा होगा।
और एक और Button
और कुछ और जो आप जोड़ सकते हैं।
निम्नलिखित मेरे लिए अच्छी तरह से काम करता है।
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff314859"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/txt_logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="your text here"
android:textSize="30dp"
android:gravity="center"/>
...other button or anything else...
</RelativeLayout>
<TextView
android:id="[email protected]/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
android:gravity="center"
यह चाल करेगा